0

In the FragmentPagerAdapter I can override destroyItem method and just remove line super.destroyItem(container, position, object);

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        //don't destory items after switching
        //super.destroyItem(container, position, object);
    }

Then fragments will no longer be deleted. But in the new FragmentStateAdapter I have not found the same method.

Dyno Cris
  • 1,823
  • 1
  • 11
  • 20
  • I don't think they intended for Fragments to not be detached. Although you could have also set the off page limit just like that on previous ViewPager too. – EpicPandaForce Feb 01 '20 at 17:44

1 Answers1

2

I found the solution. Just need add the one line to the your viewPager object:

mViewPager.setOffscreenPageLimit(numberFragmentsYouWantToKeep);

This will allow you to store the desired number of fragments in memory.

E_net4
  • 27,810
  • 13
  • 101
  • 139
Dyno Cris
  • 1,823
  • 1
  • 11
  • 20