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
    }

}
Léo Lam
  • 3,870
  • 4
  • 34
  • 44
jul
  • 36,404
  • 64
  • 191
  • 318
  • 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? – James Jan 05 '12 at 23:05
  • 1
    I just tried Mark Murphy's EU4You ( https://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. – James Jan 06 '12 at 00:23

7 Answers7

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.

James
  • 1,736
  • 2
  • 23
  • 25
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

Zephyr
  • 6,123
  • 34
  • 33
  • 3
    that's true. Fragment.onSaveInstanceState will not be called until the activity hosting it needs to save its state as stated here: http://developer.android.com/reference/android/app/Fragment.html#onSaveInstanceState(android.os.Bundle) – Pedro Andrade Sep 24 '14 at 16:25
  • 2
    Then what should I do if I would like to save the data in the current fragment? – benleung Oct 04 '15 at 14:36
  • 1
    @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. – Zephyr Oct 05 '15 at 17:32
  • 2
    @Zephyr 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. – benleung Oct 06 '15 at 00:14
  • @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) – Zephyr Oct 06 '15 at 13:48
  • 2
    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. – vanomart May 13 '16 at 08:56
9

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

Community
  • 1
  • 1
Fyodor Volchyok
  • 5,610
  • 4
  • 28
  • 45
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.

Scott
  • 1,263
  • 1
  • 12
  • 23
0

just restore Bundle in onCreate not in onCreateView

DECEMD
  • 1
  • 1
  • 1
-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'.

Yossie
  • 1
-7

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

Felix
  • 88,392
  • 43
  • 149
  • 167
  • 1
    When are you expecting `onSaveInstanceState` to be called? Maybe you're just not triggering it right. – Felix Dec 14 '11 at 12:25
  • I expect it to be called when leaving the Activity to save some data when going back to it. Is that right? – jul Dec 14 '11 at 12:43
  • This is for a completely different approach of fragment state persistence across instances: http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean) – Snicolas May 24 '13 at 08:41