简体   繁体   English

活动和片段中的onSaveInstanceState()?

[英]onSaveInstanceState() in activity and fragments?

I have an activity which contains 2 fragments. 我有一个包含2个片段的活动。 I want to save certain state of the activity and also of the fragments, in order to restore if the activity, or the fragments, are destroyed. 我想保存活动以及片段的某些状态,以便在活动或片段被破坏时恢复。

So I'm using onSaveInstanceState both in the fragments and in the activity, and take the data of the bundle passed to onCreate or onCreateView. 因此,我在片段和活动中都使用了onSaveInstanceState,并获取传递给onCreate或onCreateView的分发包的数据。

This works well besides, when the activity is destroyed. 此外,当活动被销毁时,此方法也很有效。 Then in restores it's own data, but, since in onCreate() I instantiate the adapter and the fragments again, they get no state. 然后,在恢复中它是自己的数据,但是,由于在onCreate()中,我再次实例化了适配器和片段,因此它们没有任何状态。

How do I solve this? 我该如何解决?

Thanks in advance. 提前致谢。

Most likely the cause of this, is that the Fragment 's onCreateView() runs before the Activity 's onResume() according to the Fragment lifecycle documentation: 造成这种情况的最可能原因是,根据Fragment生命周期文档, FragmentonCreateView()ActivityonResume()之前运行:

http://developer.android.com/guide/components/fragments.html#Creating http://developer.android.com/guide/components/fragments.html#Creating

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM