Android官方Training閱讀筆記 ---- Managing the Activity Lifecycle(Recreating an Activity) (四)

There are a few scenarios in which your activity is destroyed due to normal app behavior, such as when the user presses the Back 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.

開篇第一段。主要是說在一些場景中activity會因爲正常的app行爲被銷燬,比如按Back鍵退出或者程序調用finish()。app停止運行、長時間不用、前臺activity需要更多的資源,這幾種情況下系統也會殺死後臺進程。


When your activity is destroyed because the user presses Back or the activity finishes itself, the system’s concept of that Activity 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.

主要說正常行爲的銷燬,Activity實例因爲不再被需要而銷燬;非正常狀態下的銷燬,activity實例雖然也被銷燬,但是系統依然會記着它是存在的,如果用戶想要返回到這個activity,系統會根據一系列保存的數據來恢復它。


Note: In order for the Android system to restore the state of the views in your activity, each view must have a unique ID, supplied by the android:id attribute.

只有指定了id的view才能被系統保存狀態數據。


這裏寫圖片描述

As the system begins to stop your activity, it calls onSaveInstanceState() (1) so you can specify additional state data you’d like to save in case the Activity instance must be recreated. If the activity is destroyed and the same instance must be recreated, the system passes the state data defined at (1) to both the onCreate() method (2) and the onRestoreInstanceState() method (3).

activity被銷燬前會調用onSaveInstanceState()方法,有需要的話可以在裏面保存一些額外的數據;被重建的會調用onCreate()方法和onRestoreInstanceState()方法。


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.

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);
}

Restore :

@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
    }
    ...
}

or:

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);
}
發佈了62 篇原創文章 · 獲贊 23 · 訪問量 24萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章