Android Training - 管理Activity生命周期

来源:互联网 发布:影响力 知乎 编辑:程序博客网 时间:2024/05/21 15:46

The system calls onDestroy() after it has already called onPause() and onStop() in all situations except one: when you call finish() from within the onCreate() method. In some cases, such as when your activity operates as a temporary decision maker to launch another activity, you might call finish() from withinonCreate() to destroy the activity. In this case, the system immediately calls onDestroy() without calling any of the other lifecycle methods.


OnDestory() 总是在onPause和onStop后调用,唯一的特例是在onCreate中调用了finish(), 就会直接跳转到onDestroy();


<application ... >    ...    <activity        android:name="com.example.myfirstapp.DisplayMessageActivity"        android:label="@string/title_activity_display_message"        android:parentActivityName="com.example.myfirstapp.MainActivity" >        <meta-data            android:name="android.support.PARENT_ACTIVITY"            android:value="com.example.myfirstapp.MainActivity" />    </activity></application>
The android:parentActivityName attribute declares the name of this activity's parent activity within the app's logical hierarchy. The system uses this value to implement default navigation behaviors, such as Up navigationon Android 4.1 (API level 16) and higher. You can provide the same navigation behaviors for older versions of Android by using the Support Library and adding the <meta-data> element as shown here.

为Activity指明parentActivityName 主要是用于ActionBar的home按钮导航用;对低版本的系统可以使用 <meta-data> 和support library来支持;


When your activity receives a call to the onStop() method, it's no longer visible and should release almost all resources that aren't needed while the user is not using it. Once your activity is stopped, the system might destroy the instance if it needs to recover system memory. In extreme cases, the system might simply kill your app process without calling the activity's final onDestroy() callback, so it's important you use onStop() to release resources that might leak memory.


系统在特殊情况下(例如内存紧张)可能会直接kill在后台的Activity,而不调用onDestroy(), 因此必须保证在onStop()中处理会导致memory leak的资源;


Although the onPause() method is called before onStop(), you should use onStop() to perform larger, more CPU intensive shut-down operations, such as writing information to a database.

onPause执行时间过长会影响到新的ACT出现的时间,因此一些比较耗时的操作应放到onStop里面来做,例如操作SQLite;


When your activity comes back to the foreground from the stopped state, it receives a call to onRestart(). The system also calls the onStart() method, which happens every time your activity becomes visible (whether being restarted or created for the first time). The onRestart() method, however, is called only when the activity resumes from the stopped state, so you can use it to perform special restoration work that might be necessary only if the activity was previously stopped, but not destroyed.


onRestart方法只有在Act从Stoped回到Resumed的时候才会调用,can use it to perform special restoration work that might be necessary only if the activity was previously stopped, but not destroyed.  


There are a few scenarios in which your activity is destroyed due to normal app behavior, such as when the user presses theBack button or your activity signals its own destruction by calling finish(). The system may also destroy your activity if it's currently stopped and hasn't been used in a long time or the foreground activity requires more resources so the system must shut down background processes to recover memory.

When your activity is destroyed because the user presses Backor the activity finishes itself, the system's concept of thatActivity instance is gone forever because the behavior indicates the activity is no longer needed. However, if the system destroys the activity due to system constraints (rather than normal app behavior), then although the actual Activity instance is gone, the system remembers that it existed such that if the user navigates back to it, the system creates a new instance of the activity using a set of saved data that describes the state of the activity when it was destroyed. The saved data that the system uses to restore the previous state is called the "instance state" and is a collection of key-value pairs stored in a Bundle object.

Act在以下几种情况会调用onDestroy:

1. Back键导航

2. 在程序中调用了finish()

3. 因长时间处于Background而被系统kill掉,在这种情况下系统会保存一个bundle来记录Act被销毁时的状态;


Caution: Your activity will be destroyed and recreated each time the user rotates the screen. When the screen changes orientation, the system destroys and recreates the foreground activity because the screen configuration has changed and your activity might need to load alternative resources (such as the layout).


每次屏幕方向变换的时候Act会被destroy and recreate; 如果不想这样,在manifest里面声明

<activity android:name=".MyActivity"          android:configChanges="orientation|keyboardHidden"          android:label="@string/app_name">

向系统表明Act会自己处理这类变化而不需要destroy和recreate, 此时可以重写onConfigurationChanged来处理系统状态变化;


By default, the system uses the Bundle instance state to save information about each View object in your activity layout (such as the text value entered into an EditText object). So, if your activity instance is destroyed and recreated, the state of the layout is restored to its previous state with no code required by you. However, your activity might have more state information that you'd like to restore, such as member variables that track the user's progress in the activity.

系统会默认在bundle中存储每个view的状态(例如edittext中的文本)并自动帮你restore,但如果你还想将一些member var 记录下来,需要处理onSaveInstanceState(),在其中记录并在 onRestoreInstanceState()或onCreate() 从bundle中取回这些值;


To save additional data about the activity state, you must override the onSaveInstanceState() callback method. The system calls this method when the user is leaving your activity and passes it the Bundle object that will be saved in the event that your activity is destroyed unexpectedly. If the system must recreate the activity instance later, it passes the same Bundle object to both the onRestoreInstanceState() and onCreate() methods.




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:


onRestoreInstanceState()在onStart后调用,而且只有在当前有Saved bundle可用时才会调用;


原创粉丝点击