Android: Viewpager and FragmentStatePageAdapter

后端 未结 5 1186
春和景丽
春和景丽 2021-02-04 15:05

I\'m designing an app that allows users to flip between multiple pages in a ViewPager. I\'ve been struggling trying to figure out how it is possible to remove a Fragment instan

5条回答
  •  既然无缘
    2021-02-04 15:51

    I had the same problem problem and solved it by implementing these two functions

        public void onSaveInstanceState (Bundle outState)
        public void onActivityCreated (Bundle savedInstanceState)
    

    on the fragments that I wanted to save. On the first function, you should save in the Bundle the date that you need to restore the views ( in my case I had a bunch of spinner so I used an int array to save their positions). The second function, which is called when restoring your fragment, is where you implement the restoring process.

    I hope this helps. I also made my adapter to inherit from FragmentStatePageAdapter but I am not sure that this is mandatory.

提交回复
热议问题