Expected the adapter to be 'fresh' while restoring state

前端 未结 7 2442
一个人的身影
一个人的身影 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:00

    I encountered the same problem with ViewPager2. After a lot of efforts on testing different methods this worked for me:

    public void onExitOfYourFragment() {
        viewPager2.setAdapter(null);
    }
    

    When you come back to the fragment again:

    public void onResumeOfYourFragment() {
        viewPager2.setAdapter(yourAdapter);
    }
    

提交回复
热议问题