Skip to content Skip to sidebar Skip to footer

Viewpager Retaing Old Fragments On Screen Rotation

I am using ViewPager and FragmentStatePagerAdapter to show fragment. Initially there are 2 fragments added Fragment1 and Fragment2 and 3rd fragment Fragment3 is added at 2nd positi

Solution 1:

Only override this method in FragmentpagerAdapter

@OverridepublicvoiddestroyItem(ViewGroup container, int position, Objectobject) {
       // TODO Auto-generated method stubsuper.destroyItem(ViewGroup container, int position, Objectobject);
}

then remove super.destroyItem(ViewGroup container, int position, Object object); from your code

Solution 2:

I solved the problem, I am posting my solution here. I override instantiateItem of FragmentStatePagerAdapter and inside it I am getting object from super.instantiateItem and comparing its classname with classname of object at that position from list of fragments that I am maintaining.

If classname is not same its mean super.instantiateItem returns duplicate fragment. So I am calling destroyItem at that position and again calling super.instantiateItem and returning it from instantiateItem.

FragmentStatePagerAdapter return old fragment because in framework code arraylist mFragments has duplicate fragment and it just return fragment at current position. I think it should compare fragment at the position in list with currently displaying fragment before returning it.

Post a Comment for "Viewpager Retaing Old Fragments On Screen Rotation"