Can't retain nested fragments

Bogdan Zurac picture Bogdan Zurac · Feb 13, 2013 · Viewed 12.5k times · Source

Is there another way of saving the state of the nested fragment ? Or if we shouldn't do this, why ? Thanks !

02-13 11:42:43.258: E/AndroidRuntime(7167): java.lang.IllegalStateException: Can't retain fragements that are nested in other fragments
02-13 11:42:43.258: E/AndroidRuntime(7167):     at android.support.v4.app.Fragment.setRetainInstance(Fragment.java:742)

Answer

nicopico picture nicopico · Feb 13, 2013

You can use FragmentManager.saveFragmentInstanceState(Fragment) to retrieve a fragment state. The return value implements Parcelable, so you can put it in a Bundle.

For restoration, you can provide the state after creating the fragment using Fragment.setInitialSavedState(Fragment.SavedState).