onSaveInstanceState

作用

onSaveInstanceState的作用是用來保存Activity的狀態,避免再次回到Actviity時數據丟失,比較常見的是表單輸入頁面。

調用時機

假設用戶當前的頁面是ActivityA,經過測試在以下幾種情況下系統會調用onSaveInstanceState

1,用戶按下home鍵

2,用戶喚起歷史任務,并選擇某個任務(其他app)

3,用戶按下電源鍵

4,屏幕方向切換

5, 在不finish的前提下,ActivityA啟動ActivityB

生命周期

假設當前的頁面是ActivityA,按下home鍵后生命周期執行情況如下

onCreate->onStart->onResume
onPause->onSaveInstance->onStop

這時ActivityA處于后臺,根據系統的內存狀態決定了ActivityA是否會被強制銷毀。
如果系統內存不足,強制ActivityA銷毀,當用戶回到ActivityA時,生命周期如下:

onCreate->onStart-> onRestoreInstanceState->onResume

NOTE:onCreate中的bundle不為null,可以從bundle中恢復數據,也可以從onRestoreInstanceState中恢復數據
模擬Activity銷毀的方法:
1,開發者選項不保留活動
2,第三方工具的一鍵清理

如果系統內存充足,ActivityA未被銷毀,當用戶回到ActivityA時,生命周期如下:

onRestart->onStart->onResume

Activity的代碼執行情況如下:
1,performResume

final void performResume() {
        performRestart();//----------------注意

        mFragments.execPendingActions();

        mLastNonConfigurationInstances = null;

        mCalled = false;
        // mResumed is set by the instrumentation
        mInstrumentation.callActivityOnResume(this);//對應Activity的onResume
        if (!mCalled) {
            throw new SuperNotCalledException(
                "Activity " + mComponent.toShortString() +
                " did not call through to super.onResume()");
        }
        ......

        // Now really resume, and install the current status bar and menu.
        mCalled = false;

        mFragments.dispatchResume();
        mFragments.execPendingActions();
        ......
    }

2,performRestart

final void performRestart() {
        mFragments.noteStateNotSaved();

        if (mStopped) {
            mStopped = false;
            if (mToken != null && mParent == null) {
                WindowManagerGlobal.getInstance().setStoppedState(mToken, false);
            }
            ......
            mCalled = false;
            mInstrumentation.callActivityOnRestart(this);//對應Activity的onRestart
            if (!mCalled) {
                throw new SuperNotCalledException(
                    "Activity " + mComponent.toShortString() +
                    " did not call through to super.onRestart()");
            }
            performStart();//----------------注意
        }
    }

3,performStart

final void performStart() {
        mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
        mFragments.noteStateNotSaved();
        mCalled = false;
        mFragments.execPendingActions();
        mInstrumentation.callActivityOnStart(this);//對應Activity的onStart
        if (!mCalled) {
            throw new SuperNotCalledException(
                "Activity " + mComponent.toShortString() +
                " did not call through to super.onStart()");
        }
        mFragments.dispatchStart();
        mFragments.reportLoaderStart();
        mActivityTransitionState.enterReady(this);
    }

NOTE:符合前面的onRestart->onStart->onResume

結論

onSaveInstanceState和onRestoreInstanceState并不是成對出現的,在后臺的Activity有可能未被銷毀,生命周期也是有區別的

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容