android – FragmentPagerAdapter – How to handle Orientation Changes?-ThrowExceptions

Exception or error:

I use a FragmentPagerAdapter containing several Fragments that need to be notified about changes to the database. I do this by iterating over the fragments, which implement a callback interface, and calling a refreshData() method.

This works just fine until the device changes orientation. After an orientation change, the fragment UI does not visibly refresh even though the method call seems to work.

From what I have read so far this occurs because the FragmentPagerAdapter handles the fragment life-cycle and the fragments that receive the callback are not the ones that are actually displayed.

private class DataPagerAdapter extends FragmentPagerAdapter {

    private DataFragment[] fragments = new DataFragment[] {
                                     new FooDataFragment(), BarDataFragment()};

    public DataPagerAdapter( fm) {

    public Fragment getItem(int position) {
        return fragments[position];

    public int getCount() {
        return fragments.length;

    public DataFragment[] getFragments() {
        return fragments;

protected void refreshData() {
    for (DataFragment dataFragment : mPagerAdapter.getFragments()) {

I temporarily fixed this issue using a broadcast receiver inside each fragment, but this solution seems to be wasteful and might create memory leaks. How to I fix this properly? I use a different layout and logic in landscape mode so I want to use the newly created fragments after an orientation change.

How to solve:

Yes like you said FragmentManager handles fragments after orientation change so getItem in adapter is not called. But you can override method instantiateItem() which is called even after orientation change and cast Object to Fragment and save it in your array.

 public Object instantiateItem(ViewGroup container, int position) {
     DataFragment fragment = (DataFragment) super.instantiateItem(container, position);
     fragments[position] = fragment;
     return fragment;


Could not you just set the android:configChanges flag on your Activity in AndroidManifest.xml to prevent activity to be recreated?


also there you can find more possibilities:


I recreated the pager adapter in the onResume method:

override fun onResume() {
    val viewPager = findViewById<ViewPager>(
    viewPager.adapter = MyPagerAdapter(supportFragmentManager)


I had similar problem my working solution:
In activity where is an adapter call adapter.notifyDataSetChanged();

override getItemPostion() method in your adapter

    public int getItemPosition(Object object) {
        MyFragment frag = (MyFragment)object;
        return POSITION_UNCHANGED;

my FragmentStatePagerAdapater (my adapter) contains only one type of fragment, but I am think, that you could use instanceof (for more types of Fragments) and then use cast or every fragment will implement Interface with method refresh()

Then add method refresh() in your Fragment(s) and use this implementation:

public void refresh() {
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        ViewGroup viewGroup = (ViewGroup) getView();
        View view = onCreateView(inflater, viewGroup, null);


This worked for me.
My opinion:
The problem has came, when the device change orientation, adapter is destroyed, but the fragments are still in memory and system try to reuse them after orientation change, but the views which are contains in the fragments are destroyed too and fragments needs to be “recreate” (call onCreateView method ). Maybe I am wrong, so please correct me… .

(Sorry for my english )

Leave a Reply

Your email address will not be published. Required fields are marked *