Activity 的生命周期(Lifecycle)
在用户与App进行交互,开启APP,退出APP,按Home键回到桌面等等的时候,相关的Activity实例将会不断的切换Activity生命周期中的不同状态。
例如:当一个ActivityA首次启动到系统的前面可以跟用户进行交互的过程中,Android系统将会调用ActivityA生命周期中一系列的回调函数(onCreate()–onStar()–onResume())。当用户从ActivityA跳转到ActivityB或者打开另一个APP的时候,Android系统将会调用ActivityA中另一套相对应的函数(onPause()–onStop())。
在系统调用Activity生命周期不同方法的时候,可以编写代码让Activity表现想要的对应行为。
简单了解Activity的生命周期还是比较容易的,只需要记住用户可见的三种状态以及这三种状态在不同情况下如何进行转换的:
用户可见的三种状态
Resumed
在Resumed状态的时候,Activity是直接显示在用户界面上,可以跟用户进行互动的,也说是运行时Running状态。这时候对应的Activity其实位于Activity栈顶。
Paused
当ActivityA只是被ActivityB部分遮挡的时候,ActivityA将会处于Paused状态。此时ActivityA不能接受用户任何输入,以及执行任何代码。
Stopped
在Stoped状态的话,activity认为是对用户来说完全看不到,处于系统后端。但是activity当前实例所有的状态信息都将保持在Activity堆栈中,实例并未销毁只是不执行任何代码而已。
三种状态的相互切换看图应该没问题,记住部分可见处于Paused、全部不可见处于Stopped
一些细节上需要去注意:
1、当onStart()函数被Android系统调用的时候,activity就对用户可见了,同时onResume()将会迅速被系统调用,然后一直处于Resumed状态,直到状态需要改变。比如当前Activity被ActivityB部分遮挡或activity认为是对用户来说完全看不到。
2、当你在onCreate()方法中直接调用finish()方法去结束Activity的时候,系统将会立即调用 onDestroy() 方法,不会调用onPause()和onStop().
3、When the system calls onPause() for your activity, it technically means your activity is still partially visible, but most often is an indication that the user is leaving the activity and it will soon enter the Stopped state. You should usually use the onPause() callback to:
Stop animations or other ongoing actions that could consume CPU.
Commit unsaved changes, but only if users expect such changes to be permanently saved when they leave (such as a draft email).
Release system resources, such as broadcast receivers, handles to sensors (like GPS), or any resources that may affect battery life while your activity is paused and the user does not need them.
4、应该避免耗CPU的工作在onPause()中进行操作,例如写数据入数据库。因为这样会降低跳转到另一个Activity的流畅度(应该让这些操作在onStop()中进行)。
5、当Activity处于paused状态时,activity实例并未被销毁依然在内存中,当activity再次回到resumed状态过程中,不需要再去初始化组件,暂停的实例对象将会被调用。
6、在极端的情况下,系统将不调用activity的onDestroy()函数,直接kill掉app的进程。因此调用onStop()方法去释放一些容易造成内存泄漏的资源是很有必要的。
7、 当Activity被Stopped的时候,Activity对象还是在内存中的,因此Activity再次回到resumed状态时不必再去初始化组件。并且系统将会保存好各在当前Activity的layout中的View 的状态。但是当调用onStop()的时候释放的资源应该在onRestart()中重新实例化。
8、当手机屏幕旋转的时候,Activity将会被destroy和recreate。例如,当改变了屏幕的方向的时候,系统将会销毁和重建当前的Activity,因为屏幕配置已经被改变,Activity需要去加载有可能需要的资源(例如加载新的布局文件layout等)
9、当系统销毁一个Activity的时候,将会调用Activity中的onDestroy()方法。因为释放大部分资源的操作应该已经在onStop()方法中完成了,同时收到一个回调去调用onDestroy(),这个方法是最后一个机会去关闭那些容易造成内存泄漏的资源。
10、当系统开始去stopActivity的时候,将会先调用onSaveInstanceState()方法(1),该方法主要是用来保存当前Activity的一些状态信息,因此可以在该方法中保存想要保存的信息,以防该Activity再次被创建。如果Activity已经被销毁,相同的实例再次被创建的时候,系统将会将在(1)中保存的状态信息传递给onCreate() 方法 (2) 和 onRestoreInstanceState() 方法 (3).
11、As your activity begins to stop, the system calls onSaveInstanceState() so your activity can save state information with a collection of key-value pairs. The default implementation of this method saves information about the state of the activity’s view hierarchy, such as the text in an EditText widget or the scroll position of a ListView.
12、To save additional state information for your activity, you must implement onSaveInstanceState() and add key-value pairs to the Bundle object. For example:
static final String STATE_SCORE = "playerScore"; static final String STATE_LEVEL = "playerLevel"; ... @Override public void onSaveInstanceState(Bundle savedInstanceState) { // Save the user's current game state savedInstanceState.putInt(STATE_SCORE, mCurrentScore); savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel); // Always call the superclass so it can save the view hierarchy state super.onSaveInstanceState(savedInstanceState); }
13、Both the onCreate() and onRestoreInstanceState() callback methods receive the same Bundle that contains the instance state information.
Because the onCreate() method is called whether the system is creating a new instance of your activity or recreating a previous one, you must check whether the state Bundle is null before you attempt to read it. If it is null, then the system is creating a new instance of the activity, instead of restoring a previous one that was destroyed.
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Always call the superclass first // Check whether we're recreating a previously destroyed instance if (savedInstanceState != null) { // Restore value of members from saved state mCurrentScore = savedInstanceState.getInt(STATE_SCORE); mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL); } else { // Probably initialize members with default values for a new instance } ... }
14、Instead of restoring the state during onCreate() you may choose to implement onRestoreInstanceState(), which the system calls after the onStart() method. The system calls onRestoreInstanceState() only if there is a saved state to restore, so you do not need to check whether the Bundle is null:
public void onRestoreInstanceState(Bundle savedInstanceState) { // Always call the superclass so it can restore the view hierarchy super.onRestoreInstanceState(savedInstanceState); // Restore state members from saved instance mCurrentScore = savedInstanceState.getInt(STATE_SCORE); mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL); }