anujprashar anujprashar - 7 months ago 36
Android Question

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 position after recieiving response from server. So after all pages are added this should be sequence of Fragments in ViewPager --> Fragment1, Fragment3, Fragment2.

The Problem is after Fragment1 and Fragment3 are added and app is making server call before adding 3rd Fragment at 2nd position if I do screen rotation multiple time then after 3rd fragment is added it still shows old copy of Fragment2 which was at 2nd position initially at 2nd position and 3rd position have new copy of Fragment2. So Fragment3 doest shows up in ViewPager. Sequence after adding 3rd Fragment -- > Fragment1, old copy of Fragment2, New Fragment2.

I am overriding
onSaveInstanceState
and calling
super.onSaveInstanceState
in my Activity.

Also I have tried returning
POSITION_NONE
from
getItemPosition
. I read somewhere that ViewPager save copies of fragment. Also through debugging I checked that ViewPager contained 2 copies of Fragment2 when issue was reproduced even though
getItem
of
FragmentStatePagerAdapter
returned different Fragments for each position but still at 2nd position it was showing old fragment. For testing purpose in
getItem
I returned Fragment1 for all positions so that all 3 pages should be same but even after that in 2nd position it was showing old copy of Fragment2 when I reproduced issue with steps mentioned above.

So how to clear
ViewPager
so that it does not save old fragments. How to refresh ViewPager so that it does not retain old copy with fragments. I think problem is with onSaveInstanceState but I need it. How can I exclude
ViewPager
when views are saved in
onSaveInstanceState
. I have tried
mViewPager.setSaveEnabled(false)
but it takes too much memory.

I found that the problem is
instantiateItem
method of adapter does not call getItem due to which old fragment is returned.

Below is solution code

Code:

@Override
public Object instantiateItem(ViewGroup container, int position) {
Object obj = super.instantiateItem(container, position);
Fragment fragment = mFragments.get(position);

if((obj!=null && fragment!=null) && !(obj.getClass().getSimpleName().equals(fragment.clss.getSimpleName()))){
destroyItem(container, position, obj);
return super.instantiateItem(container, position);
}else{
return obj;
}
}

Answer Source

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.