简体   繁体   中英

Why is onRestoreInstanceState called for a view when the activity is destroyed?

In the stacktrace below you can notice that as a result of the activity being destroyed, the view's onRestoreInstanceState is called. Why is this necessary?

    at com.mypackage.MyView.onRestoreInstanceState(Unknown Source)
    at android.view.View.dispatchRestoreInstanceState(View.java:13758)
    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2889)
    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2895)
    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2895)
    at android.view.View.restoreHierarchyState(View.java:13736)
    at android.support.v4.app.Fragment.restoreViewState(Unknown Source)
    at android.support.v4.app.FragmentManagerImpl.moveToState(Unknown Source)
    at android.support.v4.app.FragmentManagerImpl.moveToState(Unknown Source)
    at android.support.v4.app.FragmentManagerImpl.moveToState(Unknown Source)
    at android.support.v4.app.FragmentManagerImpl.dispatchReallyStop(Unknown Source)
    at android.support.v4.app.FragmentActivity.onReallyStop(Unknown Source)
    at android.support.v4.app.FragmentActivity.doReallyStop(Unknown Source)
    at android.support.v4.app.FragmentActivity.onDestroy(Unknown Source)
    at android.support.v7.app.o.onDestroy(Unknown Source)
    at android.app.Activity.performDestroy(Activity.java:6189)
    at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1164)
    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3778)
    ... 10 more

EDIT :

The View.onRestoreInstanceState receives whatever View.onSaveInstanceState produced. The documentation for onSaveInstanceState says: "Hook allowing a view to generate a representation of its internal state that can later be used to create a new instance with that same state." - When an onDestroy is performed there is no need to recreate the views, so then why is onRestoreInstanceState called?

The onRestoreInstanceState of your Fragment happens as part of the onDestroy of your Activity .

This is the behavior in FragmentManagerImpl :

void moveToState(Fragment f, int newState, int transit, int transitionStyle, boolean keepActive) {
    //...
    case Fragment.CREATED:
    if (newState > Fragment.CREATED) {
    //...
        f.performActivityCreated(f.mSavedFragmentState);
        if (f.mView != null) {
            f.restoreViewState(f.mSavedFragmentState);
        }
        f.mSavedFragmentState = null;
    }
}

If the state of the fragment is changed (moved) and the fragment's view is not null then restoreViewState is always called as part of this process.

Maybe you are not calling super.onDestroy() as the first statement in onDestroy ?

我想问题应该是:为什么片段销毁后savedInstanceState不为null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM