關於Activity的onSaveInstanceSate()這個API

####關於onSaveInstanceSate()這個API 參考:https://developer.android.com/reference/android/app/Activity#onSaveInstanceState(android.os.Bundle)android

protected void onSaveInstanceState (Bundle outState)

Called to retrieve per-instance state from an activity before being killed so that the state can be restored in onCreate(Bundle) or onRestoreInstanceState(Bundle) (the Bundle populated by this method will be passed to both).app

This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state. For example, if activity B is launched in front of activity A, and at some point activity A is killed to reclaim resources, activity A will have a chance to save the current state of its user interface via this method so that when the user returns to activity A, the state of the user interface can be restored via onCreate(Bundle) or onRestoreInstanceState(Bundle).ide

Do not confuse this method with activity lifecycle callbacks such as onPause(), which is always called when an activity is being placed in the background or on its way to destruction, or onStop() which is called before destruction. One example of when onPause() and onStop() is called and not this method is when a user navigates back from activity B to activity A: there is no need to call onSaveInstanceState(Bundle) on B because that particular instance will never be restored, so the system avoids calling it. An example when onPause() is called and not onSaveInstanceState(Bundle) is when activity B is launched in front of activity A: the system may avoid calling onSaveInstanceState(Bundle) on activity A if it isn't killed during the lifetime of B since the state of the user interface of A will stay intact.ui

The default implementation takes care of most of the UI per-instance state for you by calling View.onSaveInstanceState() on each view in the hierarchy that has an id, and by saving the id of the currently focused view (all of which is restored by the default implementation of onRestoreInstanceState(Bundle)). If you override this method to save additional information not captured by each individual view, you will likely want to call through to the default implementation, otherwise be prepared to save all of the state of each view yourself.this

If called, this method will occur after onStop() for applications targeting platforms starting with Build.VERSION_CODES.P. For applications targeting earlier platform versions this method will occur before onStop() and there are **no guarantees** about whether it will occur before or after onPause().spa


This method is called before an activity may be killed,so that when it comes back some time in the future it can restore its state. 好比說 Activity_B在Activity_A裏被啓動了,在某個時候Activity_A被kill掉以節省資源. 可是Activity_A有一個機會保存當前的狀態,這樣當下次用戶從新回到Activity_A的時候, 以前保存的狀態信息就能夠被restored經過onCreate(Bundle) 或者onRestoreInstanceState(Bundle).rest

可是文檔同時也提醒了不要把這個方法和onPause混淆. onPause是必定會被調用的當當前的Activity被放到後臺或者銷燬的時候. onPause和onStop的調用時機和onSaveInstanceState沒什麼關係. 好比當用戶從B navigate back回A的時候,由於這個時候肯定B instance will never be restored. 因此就不會調用onSaveInstanceState,可是onPause和onStop確定會調用. 還有一個例子當onPause()被調用可是不會調用onSaveInstanceState() 一個activity B被喚起在activity A的前面,系統就不會調用A的onSaveInstanceState()方法, if it isn't killed during the lifetime of B. Since the user interface of A will stay intact.code

默認行爲是系統會調用每一個有id的view的View.onsaveInstanceState()方法保存其state. 就是說super.onSaveInstanceState(bundle)乾的就是這個事.orm

protected void onSaveInstanceState(Bundle outState) {
        outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState());

        outState.putInt(LAST_AUTOFILL_ID, mLastAutofillId);
        Parcelable p = mFragments.saveAllState();
        if (p != null) {
            outState.putParcelable(FRAGMENTS_TAG, p);
        }
        if (mAutoFillResetNeeded) {
            outState.putBoolean(AUTOFILL_RESET_NEEDED, true);
            getAutofillManager().onSaveInstanceState(outState);
        }
        getApplication().dispatchActivitySaveInstanceState(this, outState);
    }

關於調用時序 在Android P之後(ApiLevel 28),這個方法會在onStop()之後調用. 而在P以前的話保證了在onStop()以前,可是不保證是否是在onPause()以後.資源

相關文章
相關標籤/搜索