简体   繁体   中英

onSaveInstanceState() in activity and fragments?

I have an activity which contains 2 fragments. 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.

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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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