I have a viewpager2 with multiple fragments in FragmentStateAdapter. Whenever I try to open a new fragment and then go back to my current one with viewpager2, I get an exception
I've been struggling with this and none of the previous answers helped.
This may not work for every possible situation, but in my case fragments containing ViewPager2 were fixed and few, and I solved this by doing fragment switch with FragmentTransaction's show()
and hide()
methods, instead of replace()
commonly recommended for this. Apply show()
to the active fragment, and hide()
to all others. This avoids operations like re-creating views, and restoring state that trigger the problem.