【问题标题】:Why is onRestoreInstanceState called for a view when the activity is destroyed?为什么在销毁活动时为视图调用 onRestoreInstanceState?
【发布时间】:2016-02-29 13:09:57
【问题描述】:

在下面的堆栈跟踪中,您可以注意到由于 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 呢?

【问题讨论】:

  • 你的安卓版本是什么?
  • 例外是什么?
  • 异常无关。安卓版本从安卓4.0.3到安卓5.1。这个特定的来自:根据 Google Play 开发者控制台的 Moto X (gen2) (victara)。
  • 你的 compileSdkVersion 是什么?
  • compileSdkVersion=22 在 build.gradle 中。我是说异常无关紧要,因为它是我自定义视图的 onRestoreInstanceState 中的自定义代码中的 NPE - 我现在已修复它并且异常不再出现。

标签: android


【解决方案1】:

FragmentonRestoreInstanceStateActivityonDestroy 的一部分。

这是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 中的第一条语句调用?

【讨论】:

  • View.onRestoreInstanceState 接收 View.onSaveInstanceState 生成的任何内容。 onSaveInstanceState 的文档说:“钩子允许视图生成其内部状态的表示,以后可以用来创建具有相同状态的新实例。” - 执行 onDestroy 时不需要重新创建视图,那么为什么要调用 View.onRestoreInstanceState 呢?能否请您详细回答,谢谢!
  • 你的问题是什么?
  • 也许您没有将super.onDestroy() 作为onDestroy 中的第一条语句调用?
  • 你从堆栈跟踪中删除了哪一行?
  • 请编辑您的问题以包含整个跟踪。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 2012-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多