android 片段 onRestoreInstanceState

发布于 2024-10-25 12:13:48 字数 103 浏览 7 评论 0原文

我是否遗漏了某些内容或者 Fragment 没有 onRestoreInstanceState() 方法?如果没有,我该如何获得类似的东西?

Am I missing something or do Fragments not have a onRestoreInstanceState() method? If not, how do I go about attaining something similar?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

玩物 2024-11-01 12:13:48

片段没有 onRestoreInstanceState 方法。

您可以在 onActivityCreated 中实现相同的结果,它接收具有已保存实例状态(或 null)的包。

请在此处查看源代码。

Fragments do not have an onRestoreInstanceState method.

You can achieve the same result in onActivityCreated, which receives a bundle with the saved instance state (or null).

Check the source code here.

剧终人散尽 2024-11-01 12:13:48

我知道,您已经接受了答案,但是您应该阅读有关片段的官方文档,并且它说(“处理片段生命周期”段落):

您可以使用 Bundle 保留片段的状态,以防活动的进程被终止并且您需要在重新创建活动时恢复片段状态。您可以在片段的 onSaveInstanceState() 回调期间保存状态,并在 onCreate()、onCreateView() 或 onActivityCreated() 期间恢复状态

,因此,您可以使用最适合您的:onCreate()onCreateView()onActivityCreated()

I know, that you have accepted answer, but you should read the official documentation about fragments, and it says (paragraph "Handling the Fragment Lifecycle"):

You can retain the state of a fragment using a Bundle, in case the activity's process is killed and you need to restore the fragment state when the activity is recreated. You can save the state during the fragment's onSaveInstanceState() callback and restore it during either onCreate(), onCreateView(), or onActivityCreated()

So, you can use that suits you best: onCreate(), onCreateView(), or onActivityCreated()

少女的英雄梦 2024-11-01 12:13:48

在 Fragments 指南的 ListFragment 示例 中,您可以找到:

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("curChoice", mCurCheckPosition);
}

您可以像这样使用:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (savedInstanceState != null) {
        // Restore last state for checked position.
        mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
    }
}

onActivityCreated () 在片段从堆栈返回后被调用。

In Fragments guide's ListFragment example you can find:

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("curChoice", mCurCheckPosition);
}

Which you can use like this:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (savedInstanceState != null) {
        // Restore last state for checked position.
        mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
    }
}

onActivityCreated() is invoked after the fragment returns back from the stack.

℡寂寞咖啡 2024-11-01 12:13:48

onViewStateRestored of Fragment is the equivalent of onRestoreInstanceState of Activity. But it is called after onActivityCreated(Bundle) and before onStart().

枕梦 2024-11-01 12:13:48

onActivityCreated 已弃用。我发现它在片段生命周期方面令人困惑。只需执行以下操作:

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("curChoice", mCurCheckPosition);
}

// 然后:

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
    savedIInistanceState?.let{ 
//restore the data here
    }
    }

onActivityCreated is deprecated. and i found it just confusing in terms of the fragment lifecycle. just do this:

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("curChoice", mCurCheckPosition);
}

// and then:

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
    savedIInistanceState?.let{ 
//restore the data here
    }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文