Chao Chao - 1 year ago 142
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

), 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.


Answer Source

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.