关于Activity的onSaveInstanceSate()这个API

####关于onSaveInstanceSate()这个API 参考:https://developer.android.com/reference/android/app/Activity#onSaveInstanceState(android.os.Bundle)

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).

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).

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.

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.

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().


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).

但是文档同时也提醒了不要把这个方法和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.

默认行为是系统会调用每个有id的view的View.onsaveInstanceState()方法保存其state. 就是说super.onSaveInstanceState(bundle)干的就是这个事.

    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()之后.

转载于:https://my.oschina.net/tanghaoo/blog/2050962

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值