[英]Will onRestoreInstanceState() still be called when activity is recreated?
[英]Why is onRestoreInstanceState called for a view when the activity is destroyed?
在下面的堆栈跟踪中,您可以注意到,由于 Activity 被销毁,视图的 onRestoreInstanceState 被调用。 为什么这是必要的?
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
编辑:
View.onRestoreInstanceState接收任何View.onSaveInstanceState产生。 onSaveInstanceState 的文档说:“钩子允许视图生成其内部状态的表示,以后可用于创建具有相同状态的新实例。” - 当执行 onDestroy 时,不需要重新创建视图,那么为什么要调用 onRestoreInstanceState 呢?
您的Fragment的onRestoreInstanceState
作为Activity的onDestroy
的一部分发生。
这是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;
}
}
如果片段的状态被改变(移动)并且片段的视图不为空,那么作为这个过程的一部分,restoreViewState 总是被调用。
也许您没有将super.onDestroy()
称为onDestroy
的第一条语句?
我想问题应该是:为什么片段销毁后savedInstanceState
不为null
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.