Double fragment rotating Android with ActionBar

后端 未结 4 722
轻奢々
轻奢々 2020-12-24 08:33

I\'ve made a simple Android Activity with an ActionBar to switch between 2 fragments. It\'s all ok until I rotate the device. In facts, when I rotate I\'ve got 2 fragment on

4条回答
  •  一个人的身影
    2020-12-24 08:57

    Since I use a android.support.v4.view.ViewPager overriding onTabSelected would not help. But still you hint pointed me in the right direction.

    The android.support.v4.app.FragmentManager saves all fragments in the onSaveInstanceState of the android.support.v4.app.FragmentActivity. Ignoring setRetainInstance — Depending on your design this might lead to duplicate fragments.

    The simplest solution is to delete the saved fragment in the orCreate of the activity:

       @Override
       public void onCreate (final android.os.Bundle savedInstanceState)
       {
          if (savedInstanceState != null)
          {
             savedInstanceState.remove ("android:support:fragments");
          } // if
    
          super.onCreate (savedInstanceState);
    …
          return;
       } // onCreate
    

提交回复
热议问题