Fragment's onSaveInstanceState() is never called

后端 未结 6 1136
醉酒成梦
醉酒成梦 2020-11-30 10:31

I\'m trying to save data in a Fragment\'s onSaveInstanceState(), but the method is never called.

Can someone help?

public class MyFragment extends Fr         


        
相关标签:
6条回答
  • 2020-11-30 11:12

    One thing to check is to make sure the Activity that contains the fragment is not preventing a restart by including the android:configChanges flag in the AndroidManifest.xml.

    0 讨论(0)
  • 2020-11-30 11:18

    I encountered the same question with you, and tried onSaveInstanceState() method, but did not work.

    I think onSaveInstanceState() only works for the scenario that user jumps from one activity to another activity and back, it does not work in the scenario that user jumps among fragments in the same activity.

    here is the guide document from Google. http://developer.android.com/guide/components/tasks-and-back-stack.html#ActivityState

    0 讨论(0)
  • 2020-11-30 11:24

    I finally figured out the problem, at least in my case. I had an overridden onSaveInstanceState in my FragmentActivity that did not call super.onSaveInstanceState(Bundle outState). Once I added that in, the Fragment.onSaveInstanceState(Bundle outState) functioned normally.

    0 讨论(0)
  • 2020-11-30 11:33

    In some situations you might find it helpful to use fragment arguments instead of savedInstanceState. Further explanation.

    0 讨论(0)
  • 2020-11-30 11:33

    Try calling FragmentManager#saveFragmentInstanceState and Fragment#setInitialSavedState in Activity. You call saveFragmentInstanceState, then framework will call onSaveInstanceState. And you call setInitialSavedState, then framework will call onCreateView with no null argument 'Bundle savedInstanceState'.

    0 讨论(0)
  • 2020-11-30 11:33

    Try calling setRetainInstance(true) in onCreate(Bundle savedInstanceState).

    0 讨论(0)
提交回复
热议问题