Prevent Fragment recovery in Android

前端 未结 7 1779
星月不相逢
星月不相逢 2020-12-07 22:53

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 save

7条回答
  •  情书的邮戳
    2020-12-07 23:21

    For an app with a ViewPager, I remove the fragments in onCreate(), before their creation.

    Based on this thread: Remove all fragments from container, we have:

    FragmentManager fm = getSupportFragmentManager();
    for (Fragment fragment: fm.getFragments()) {
      fm.beginTransaction().remove(fragment).commitNow();
    }
    

提交回复
热议问题