Fragment's onSaveInstanceState() is never called

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
    }

}

Solution 1:

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.

Solution 2:

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

Solution 3:

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

Solution 4:

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.