Android深入了解Activity

一、Activity简述

1、概念引入

上图为Activity类图结构,Activity作为Android的四大组件之一,Activity在Android系统中是以界面的形式进行体现。其中Activity实现了如Window.Callback, KeyEvent.Callback等接口用于与用户进行交互。

2、源码释义

An activity is a single, focused thing that the user can do.

一个界面是开发者可以操作的一个重点的独立事项。

 Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View). 
几乎所有界面都可以与用户交互,所以Activity类负责为用户创建一个窗口,你可以在其中使用setContentView(View)放置UI。

 While activities are often presented to the user as full-screen windows, they can also be used in other ways: as floating windows (via a theme with windowIsFloating set) or embedded inside of another activity (using ActivityGroup). 

虽然界面通常以全屏窗口的形式呈现给用户,但它们也可以以其他方式使用:作为浮动窗口(通过具有windowIsFloating集合的主题)或嵌入另一个活动(使用ActivityGroup)内部。

There are two methods almost all subclasses of Activity will implement:

几乎所有的Activity子类都会实现两种方法:

onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(int) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.
onCreate(Bundle)是初始化界面的地方。最重要的是,在这里你通常会调用setContentView(int)和一个定义你的UI的布局资源,并且使用findViewById(int)来检索你需要以编程方式进行交互的那个UI中控件。

onPause() is where you deal with the user leaving your activity.Most importantly, any changes made by the user should at this point be committed (usually to the ContentProvider holding the data).
onPause()是你对于用户离开界面的处理。最重要的是,此时用户所做的任何更改都应该提交(通常发送给持有数据的ContentProvider。

To be of use with Context.startActivity(), all activity classes must have a corresponding declaration in their package's AndroidManifest.xml.
要使用Context.startActivity(),所有界面类必须在其包下的AndroidManifest.xml中具有相应的声明。

 

二、Activity的生命周期

1、Activity的生命周期图

2、生命周期详解

2.1、OnCreate()方法

Called when the activity is first created. This is where you should do all of your normal static set up:
create views, bind data to lists, etc. 

在第一次创建活动时调用。 这是你应该完成所有常规静态设置的位置:创建视图,将数据绑定到列表等。

This method also provides you with a Bundle containing the activity's previously frozen state, if there was one.Always followed by onStart().
此方法还会为你提供一个包含Activity先前保存页面状态的Bundle对象(如果有的话)。总是跟着onStart()方法。

 2.2、OnRestart()方法

Called after your activity has been stopped, prior to it being started again.
在你的界面停止后调用,然后再次启动。

Always followed by onStart()

总是跟着onStart()

 2.3、OnStart()方法

Called when the activity is becoming visible to the user.

当界面对用户变得可见时调用。

Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.

如果活动进入前台,则跟随执行onResume()方法,如果隐藏,则执行onStop()。
此时,Activity实际上就已经可见了,但是还没有出现在前台,无法和用户进行交互。

 2.4、onResume()方法

Called when the activity will start interacting with the user.

当界面对用户变得可见时调用。

At this point your activity is at the top of the activity stack, with user input going to it.Always followed by onPause().

如果界面进入前台,则跟随onResume()方法,如果隐藏,则执行onStop()方法。
此时,Activity已经可见,并且出现在前台并开始活动。要注意的是onStart()被执行时Activity显示在后台,只有当OnResume()被执行时Activity才显示到前台。

 2.5、onPause()方法

Called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. Implementations of this method must be very quick because the next activity will not be resumed until this method returns.

onPause()方法在系统即将开始显示之前的界面时调用。 这通常用于将未保存的更改进行保存,并保存为持久化数据,并且会执行例如停止动画和其他可能消耗CPU的内容等。此方法的实现必须非常快速,因为在此方法返回之前,下一个界面不会显示。

Followed by either onResume() if the activity returns back to the front, or onStop() if it becomes invisible to the user.

如果界面返回到前台,则跟随onResume();如果对用户不可见,则使用onStop()

onPause()方法执行后,Activity界面切换为后台程序

 2.6、onStop()方法

Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one. This may happen either because a new activity is being started, an existing one is being brought in front of this one, or this one is being destroyed.

onStop()方法会在当界面对用户不再可见时调用,因为另一项界面正在显示并要去覆盖这个界面。 这可能是因为一项新的界面正在开始,其他的界面会被显示为前台界面,或者这个界面正在被摧毁。

Followed by either onRestart() if this activity is coming back to interact with the user, or onDestroy() if this activity is going away.

如果这个界面快速的回显与用户交互紧接着onRestart()会被执行,否则,这个界面回销毁onDestroy()方法会被执行。

 2.7、onDestroy()方法

The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.)

在你的界面被销毁前被最后调用的方法。 这可能是因为界面正在结束(有人在其中调用finish()方法因为系统会暂时销毁此Activity界面的实例以节省空间,你可以使用isFinishing()方法区分内存空间是否节省资源的这两种情况)。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值