In portrait mode, my ViewPager has 3 fragments A, B, C, but in landscape mode it has only 2 fragments A and C. Therefore, I create 2 FragmentStatePagerAdapter for each mode. The problem is changing the orientation of the screen, ViewPager restores and uses the previous fragments of the old orientation. For example, when changing the orientation from portrait to landscape, the ViewPager now shows 2 fragments A, B instead of A and C. I know why this happens, but cannot find a good solution for this.
My current workaround is to use different identifiers for the ViewPager (for example: id / viewpager_portrait for the portrait and id / viewpager_landscape for the landscape layout) to prevent reusing fragments, but this causes me a memory leak because the old fragment will not be destroyed and that's it still stored in memory.
I tried some workaround, for example, call super.onCreate (null) in onCreate activity, or delete ViewPager fragments in the ViewPager action, but all of them cause my application to crash.
So my question is how to avoid reusing one or more fragments in the FragmentStatePagerAdapter when changing orientation?
Any help would be appreciated. Thanks in advance.
source share