Chao Chao - 5 months ago 76
Android Question

ViewPager doesn't always refresh when setAdapter is called, FragmentStatePagerAdapter

First of all, I am using FragmentStatePagerAdapter to feed a ViewPager with fragments to display.

When app is in the running state (i.e. after

onResume()
), calling setAdapter on the ViewPager will always work and make my ViewPager refresh, the
getItem(int position)
method in the adapter is called.

However after an orientation change, if I call setAdapter in the
onCreate(Bundle savedInstance)
method of my activity, the
getItem(int position)
method is not called, and the old fragment is reused.

I am thinking maybe the FragmentManager is doing something that I don't understand? The Fragment Manager is the only thing that doesn't get destroyed during the orientation change.

Thanks

Answer

It could be because you are using getFragmentManager() to instantiate the FragmentStatePagerAdapter while using nested fragments.

Try using getChildFragmentManager() instead:

CustomFragmentPagerAdapter fragmentPagerAdapter = new CustomFragmentPagerAdapter(getChildFragmentManager());

Answer was found here.

Comments