Expected the adapter to be 'fresh' while restoring state

前端 未结 7 2352
一个人的身影
一个人的身影 2021-02-20 10:24

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

7条回答
  •  青春惊慌失措
    2021-02-20 11:05

    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.

提交回复
热议问题