Prevent Fragment recovery in Android

goRGon picture goRGon · Mar 20, 2013 · Viewed 9.9k times · Source

We are using Fragments and we don't need them to be automatically recovered when the Activity is recreated. But Android every time when Activity::onCreate(Bundle savedInstanceState) -> super.onCreate(savedInstanceState) is called, restores Fragments even if we use setRetainInstance(false) for those Fragments.

Moreover, in those Fragments Fragment.performCreateView() is called directly without going through Fragment::onAttach() and so on. Plus, some of the fields are null inside restored Fragment...

Does anybody know how to prevent Android from restoring fragments?

P.S. We know that in case of recreating Activity for config changes it could be done by adding to manifest android:configChanges="orientation|screenSize|screenLayout. But what about recreating activity in case of automatic memory cleaning?

Answer

goRGon picture goRGon · Mar 28, 2013

We finished by adding to activity:

@Override
public 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.