android 片段 onRestoreInstanceState
我是否遗漏了某些内容或者 Fragment
没有 onRestoreInstanceState()
方法?如果没有,我该如何获得类似的东西?
Am I missing something or do Fragment
s not have a onRestoreInstanceState()
method? If not, how do I go about attaining something similar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
片段没有
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.
我知道,您已经接受了答案,但是您应该阅读有关片段的官方文档,并且它说(“处理片段生命周期”段落):
,因此,您可以使用最适合您的:
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"):
So, you can use that suits you best:
onCreate()
,onCreateView()
, oronActivityCreated()
在 Fragments 指南的 ListFragment 示例 中,您可以找到:
您可以像这样使用:
onActivityCreated ()
在片段从堆栈返回后被调用。In Fragments guide's ListFragment example you can find:
Which you can use like this:
onActivityCreated()
is invoked after the fragment returns back from the stack.onViewStateRestored
<Fragment
的 /a> 相当于Activity
的onRestoreInstanceState
。但它是在onActivityCreated(Bundle)
之后、onStart()
之前调用的。onViewStateRestored
ofFragment
is the equivalent ofonRestoreInstanceState
ofActivity
. But it is called afteronActivityCreated(Bundle)
and beforeonStart()
.onActivityCreated 已弃用。我发现它在片段生命周期方面令人困惑。只需执行以下操作:
// 然后:
onActivityCreated is deprecated. and i found it just confusing in terms of the fragment lifecycle. just do this:
// and then: