Why isn't my fragments onSaveInstanceState() being called?

I have a fragment which has its own state (selected buttons, etc). That state is lost on a screen rotation.

The activity that contains the fragment has a portrait layout in /res/layout/, and an almost identical landscape layout in /res/layout-land/. Both layouts include the fragment like so:

  • Using Gradle with an existing Android project
  • App Widget does not appear in the widget list on Honeycomb devices until Launcher is restarted
  • How to make AUTO_INCREMENT on Android SQLite database?
  • Call requires API level 23 (current min is 14): android.app.Activity#requestPermissions,checkSelfPermission
  • Launch android application from a browser link
  • Android studio format code won't auto break line
  • <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            class="au.com.x.y.TestFragment" />

    The fragment class I’ve been testing with is:

    public class TestFragment extends android.support.v4.app.Fragment {
        private static final String TAG = "TestFragment";
        public void onCreate(Bundle savedInstanceState) {
            Log.i(TAG, "onCreate(): " + 
                    (savedInstanceState != null ? "NOT NULL" : "NULL"));
        public void onActivityCreated(Bundle savedInstanceState) {
            Log.i(TAG, "onActivityCreated(): " + 
                    (savedInstanceState != null ? "NOT NULL" : "NULL"));
        public void onSaveInstanceState(Bundle state) {
            Log.i(TAG, "onSaveInstanceState()");
            state.putString("saved_thing", "some_value");
        public View onCreateView(
            LayoutInflater inflater,
            ViewGroup container,
            Bundle b) { ... }

    Note that I’m using the pre-3.0 support package for fragments, brought in through ActionBarSherlock.

    LogCat gives me:

    02-23 11:45:58.015: I/TestFragment(22629): onCreate(): NULL
    02-23 11:45:58.015: I/TestFragment(22629): onCreateView()
    02-23 11:45:58.035: I/TestFragment(22629): onActivityCreated(): NULL
    02-23 11:46:00.615: I/TestFragment(22629): onCreate(): NULL
    02-23 11:46:00.615: I/TestFragment(22629): onCreateView()
    02-23 11:46:00.635: I/TestFragment(22629): onActivityCreated(): NULL

    As you can see, onSaveInstanceState() is never called, and the fragment always gets a null savedInstanceState, in both onCreate() and onActivityCreated().

    I’ve tried myFragment.setRetainInstance(true) in my activities onCreate(), but that hasn’t changed anything.

    My onSaveInstanceState() has an @Override, so I know it’s not something stupid like a typo.

    I’ve looked at one of the ActionBarSherlock examples (com.actionbarsherlock.sample.shakespeare) and the fragments there are having their onSaveInstanceState() methods called properly. As far as I can tell, that example is written exactly how my code is — fragments included through both a layout and a layout-land XML. I’ve even built that sample using exactly the same version of ActionBarSherlock as my main project is using, and saving the instance state works fine for that example.

    How do I retain my fragments state across screen rotations? Why isn’t onSaveInstanceState() being called?

    Related posts:

    List of files in assets folder and its subfolders
    How to configure proguard for javascript interface?
    Android: Issue with newView and bindView in custom SimpleCursorAdapter
    Android Toolbar moves up when keyboard appears
    Does android webview browsers support html5 features?
    Center content in scroll view
  • Android: Tabs at the BOTTOM
  • Hex transparency in colors
  • Android keypad key press overlay
  • Streaming video from Android camera to server
  • Android import java.nio.file.Files; cannot be resolved
  • How can I remove title and icon completetly in Actionbar sherlock?
  • 2 Solutions collect form web for “Why isn't my fragments onSaveInstanceState() being called?”

    You can enable myFragment.setRetainInstance(true) to retain the state, but it does this ‘automatically’, i.e. it retains the values assigned to your class members and does NOT use the onSaveInstanceState (and hence savedInstanceState is always null).

    Make sure the FragmentActivity that hosts this fragment does not override onSaveInstanceState or when it does it should call super.onSaveInstanceState(Bundle).

    If the fragment needs a different layout resource when rotated, setRetainInstance(true) cannot be used. This page shows the lifecycle of fragment with setRetainInstance(true).

    Original idea is use onSaveInstanceState() to retain all members data, just like a regular Activity. However, for some reason, onSaveInstanceState() is not get called. But FragmentActivity is correct, as @Eric Kok suggested). The best solution I found is to use Arguments of Fragment.

    See the solution by Fyodor Volchyok, it is simple. Just work as if the outState in onSaveInstanceState(), and savedInstanceState in onViewCreated().

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.