[英]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.在下面的堆栈跟踪中,您可以注意到,由于 Activity 被销毁,视图的 onRestoreInstanceState 被调用。 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. View.onRestoreInstanceState接收任何View.onSaveInstanceState产生。 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."
onSaveInstanceState 的文档说:“钩子允许视图生成其内部状态的表示,以后可用于创建具有相同状态的新实例。” - When an onDestroy is performed there is no need to recreate the views, so then why is onRestoreInstanceState called?
- 当执行 onDestroy 时,不需要重新创建视图,那么为什么要调用 onRestoreInstanceState 呢?
The onRestoreInstanceState
of your Fragment happens as part of the onDestroy
of your Activity .您的Fragment的
onRestoreInstanceState
作为Activity的onDestroy
的一部分发生。
This is the behavior in FragmentManagerImpl
:这是
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.如果片段的状态被改变(移动)并且片段的视图不为空,那么作为这个过程的一部分,restoreViewState 总是被调用。
Maybe you are not calling super.onDestroy()
as the first statement in onDestroy
?也许您没有将
super.onDestroy()
称为onDestroy
的第一条语句?
我想问题应该是:为什么片段销毁后savedInstanceState
不为null
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.