ViewPager + FragmentStatePagerAdapter + orientation change

后端 未结 3 1725
广开言路
广开言路 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<Fragment> 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

    0 讨论(0)
  • 2020-12-09 19:53

    You can override the FragmentActivity onSaveInstanceState() and not call the super.onSaveInstanceState() in the method.

    @Override
    protected void onSaveInstanceState(final Bundle outState) {
        // super.onSaveInstanceState(outState);
    }
    

    Do not use mViewPager.setSaveEnabled(false);

    it ended up with a big memoryLeak. After every change of the orientation it puts the Fragments into an array of FragmentManager and never cleaned it. So it goes up to over 100mb memory usage after change the orientation many times. Using the onSaveInstanceState method is a better way I think.

    0 讨论(0)
  • 2020-12-09 19:57

    Just you use in viewPager.setonpagechangelistener and implement for require code apply your application,

    http://developer.android.com/reference/android/support/v4/view/ViewPager.html

    0 讨论(0)
提交回复
热议问题