Prevent Fragment recovery in Android Prevent Fragment recovery in Android android android

Prevent Fragment recovery in Android


We finished by adding to activity:

@Overridepublic void onCreate(Bundle savedInstanceState) {    super.onCreate(null);}

It suppresses any saved data on create/recreate cycle of an Activity and avoids fragments auto re-creation.


@goRGon 's answer was very useful for me, but such use cause serious problems when there is some more information you needs to forward to your activity after recreate.

Here is improved version that only removes "fragments", but keep every other parameters.

ID that is removed from bundle is part of android.support.v4.app.FragmentActivity class as FRAGMENTS_TAG field. It may of course change over time, but it's not expected.

@Overridepublic void onCreate(Bundle savedInstanceState) {    super.onCreate(createBundleNoFragmentRestore(savedInstanceState));}/** * Improve bundle to prevent restoring of fragments. * @param bundle bundle container * @return improved bundle with removed "fragments parcelable" */private static Bundle createBundleNoFragmentRestore(Bundle bundle) {    if (bundle != null) {        bundle.remove("android:support:fragments");    }    return bundle;}


I was having a problem with TransactionTooLargeException. So thankfully after using tolargetool I founded that the fragments (android:support:fragments) were been in memory, and the transaction became too large. So finally I did this, and it worked great.

@Overridepublic void onSaveInstanceState(final Bundle outState) {    super.onSaveInstanceState(outState);    outState.putSerializable("android:support:fragments", null);}

Edit: I added it to the Activity. In my case I have one single Activity app and Multiple Fragments.