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;
}
}