ViewPager2 - fragment layout disappears after multiple swipes via tabs

Viewed 20

I'm using ViewPager2 which is hosted inside a fragment. On the initial opening of ViewPager2 fragment host everything is loaded and displayed correctly, but when I swipe multiple times (via TabLayout) to the last fragment and go back to the first fragment, its layout is empty/white screen.

The adapter which I'm using is FragmentStateAdapter. Also, I set offscreenPageLimit to a constant value (which is in most cases less than the size of the fragments list in ViewPager2).

This is constructor of my custom FragmentStateAdapter:

pagerAdapter = MyCustomPagerAdapter(requireContext(), childFragmentManager, viewLifecycleOwner.lifecycle)

Since ViewPager2 is using RecyclerView internally, maybe there is some problem with recycling fragments when there are not visible (also considering offscreenPageLimit value).

1 Answers

Problem was in by viewBinding() method for getting binding object.

After using standard way of creating binding object everything is working OK.

Related