ViewPager + FragmentStatePagerAdapter + orientation change

后端 未结 3 1744
广开言路
广开言路 2020-12-09 19:20

I have a little Problem: i have a ViewPager with some Pages and i use the FragmentStatePagerAdapter to handle the data. In portrait i have for example 20 pages for the ViewP

3条回答
  •  孤城傲影
    2020-12-09 19:40

    I also occurred this problem and I found the reason.

    Ever screen rotated, The FragmentManager will retain all your activity hosted fragments, It wou't remove fragments If using FragmentStatePagerAdapter, You can use FragmentManager.getFragments() to check the number of fragments.

    Example: The activity has 4 fragments, After 4 times orientation change, The list FragmentManager.getFragments() size will be 20!

    I found some solutions:

    1. Do not call the super.onSaveInstanceState() of you Activity, But It's seems not a good solution, Fragments can't save state and restore.

    2. Use FragmentPagerAdapter instead of FragmentStatePagerAdapter. But after orientation changed, The fragments which ViewPager hosted was blanked(Does anyone else has same problem?). But I can remove all fragments in Activity.onCreate to fix it. like this:

      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          FragmentManager fm = getSupportFragmentManager();
          List fragments = fm.getFragments();
          if (fragments != null) {
              FragmentTransaction transaction = fm.beginTransaction();
              for (Fragment fragment : fragments) {
                  transaction.detach(fragment).remove(fragment);
              }
              transaction.commitNowAllowingStateLoss();
          }
          ...
      

      But the Fragment.onCreateView() called twice.

    I'm still looking for the perfect solution :D

    Edit:
    I finally solved the problem, It's cause by my ViewPager is add after onStart. When run to onStart, The FragmentManager will try to restore fragments state. I traced into FragmentManager, Have these code:

    com/android/support/support-fragment/24.2.1/support-fragment-24.2.1-sources.jar!/android/support/v4/app/FragmentManager.java

    Check the highlight line, The f.mContainerId actually is my ViewPager id. Then it will try to add fragments view to ViewPager, In my case, The ViewPager is not add yet, So it will be blanked, But fragments state are ATTACHED. And in FragmentPagerAdapter.instantiateItem(), If the fragment was added, It just do attach the fragment, But the fragment is already attached, So its do nothing :(

    I add ViewPager to activity layout XML, its fixed.

    Happy coding :D

提交回复
热议问题