The issue probably is that the built-in PagerAdapter
implementations for Fragment
s provided by Android assume that the items will remain constant, and so retain and reuse index-based references to all Fragment
s that are added to the ViewPager
. These references are maintained through the FragmentManager
even after the Activity
(and Fragment
s) is recreated due to configuration changes or the process being killed.
What you need to do is to write your own implementation of PagerAdapter
that associates a custom tag with each Fragment
and stores the Fragment
s in a tag-based (instead of index-based) format. You could derive a generic implementation of this from one of the existing ones after adding an abstract method for providing a tag based on the index alongside the getItem()
method. Of course, you will have to remove orphaned/unused Fragment
s added in the previous configuration from the ViewPager
(while ideally holding on to it's state).
If you don't want to implement the whole solution yourself, then the ArrayPagerAdapter
in the CWAC-Pager library can be used to provide a reasonable implementation of this with little effort. Upon initialization, you can detach the relevant Fragment
based on it's provided tag, and remove/add it from the adapter as well, as appropriate.