Fragment's onSaveInstanceState() is never called
Asked Answered
G

7

69

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

Can someone help?

public class MyFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ScrollView content = (ScrollView) inflater.inflate(R.layout.content, container, false);
        // More stuff
        return content;
    }

    @Override
    public void onSaveInstanceState(Bundle icicle) {
        // NEVER CALLED
        super.onSaveInstanceState(icicle);
        //More stuff
    }

}
Grizel answered 14/12, 2011 at 11:19 Comment(2)
are you using actionbar sherlock? I just checked to make sure I had the latest support package, and realized I'm using actionbar sherlock. I did update to the latest ABS, but I'm still having the problem. Not sure if maybe ABS is the culprit?Terraqueous
I just tried Mark Murphy's EU4You ( github.com/commonsguy/cw-android/tree/master/Fragments/EU4You_6 ) with the normal support library and also with ActionBarSherlock. It called onSaveInstanceState and passed the bundle to onActivityCreated as expected in both cases. So, still need to id the cause of this.Terraqueous
T
70

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.

Terraqueous answered 6/1, 2012 at 1:34 Comment(0)
P
34

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

Papal answered 2/1, 2014 at 21:14 Comment(6)
that's true. Fragment.onSaveInstanceState will not be called until the activity hosting it needs to save its state as stated here: developer.android.com/reference/android/app/…Shinbone
Then what should I do if I would like to save the data in the current fragment?Rutan
@benleung, I would like to use an object which has an independent life cycle compared to the fragment to save the data. For example, a singleton data manager, a sticky service, or even the SharedPreferences or a File, the last two may not be the best choices due to the overhead.Papal
@Papal Your suggestion might not suit my case, I really want the same set of data when I swap out and swap back the fragment. But I would prefer a new set of data if the activity and fragment are freshly created. It seems that it would difficult to distinguish this two cases.Rutan
@benleung, it maybe not that difficult, you can tell an activity is freshly created if the onCreated() is called for the activity, and you can invalidate the cached data when the onDestroy() is called for the activity, right ? (I used this method in my project)Papal
You can use onStop and onResume callbacks (of the fragment, not the activity). These two work even if you are switching between fragments on the same activity. If your app goes to the background, onSaveInstanceState and onRestoreInstanceState are called so you can you them. If they are by some circumstances not called, you might actually pass some information using method setArguments() and this information should preserve even if your app is in the background and you need to recreate the fragment again. If you don't need this functionality just store the data as regular fields.Curricle
B
9

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

Bodyguard answered 2/8, 2014 at 14:48 Comment(0)
S
6

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.

Sour answered 14/12, 2011 at 12:4 Comment(0)
R
0

just restore Bundle in onCreate not in onCreateView

Rerun answered 7/1, 2023 at 8:14 Comment(0)
A
-1

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'.

Aggrandize answered 17/10, 2013 at 8:34 Comment(0)
T
-7

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

Telemeter answered 14/12, 2011 at 11:23 Comment(3)
When are you expecting onSaveInstanceState to be called? Maybe you're just not triggering it right.Telemeter
I expect it to be called when leaving the Activity to save some data when going back to it. Is that right?Grizel
This is for a completely different approach of fragment state persistence across instances: developer.android.com/reference/android/app/…Phyllis

© 2022 - 2024 — McMap. All rights reserved.