1

In my application I have a View pager that rotates 4 Fragments. Some of those fragments have other fragments inside them. The first time Viewpager starts a Fragment it works fine, but if I rotate around and return to the previous fragment, the inner fragments fail to show ( and I have noticed that getActivity() in them returns null).

For example, the app starts, I move through viewpager and see all the main fragments and they appear fine, then I scroll back, every main fragment that has inner fragments does not show properly.

I use "FragmentStatePagerAdapter" for my ViewPager and the code is below (variables names are for example).

private class ContentPagerAdapter extends FragmentStatePagerAdapter {

    public ContentPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public Fragment getItem(int pos) {

        Fragment fragment = null;
        try {
            if (pos == POS1) {
                if (fragment1 == null) {
                    fragment1 = Fragment1.newInstance();
                }
                fragment = fragment1;
            } else if (pos == POS2) {
                if (fragment2 == null) {
                    fragment2 = Fragment2.newInstance(something);
                }
                fragment = fragment2;
            } else if (pos == POS3) {
                if ( fragment3 == null ) {
                    fragment3 = Fragment3.newInstance(something3);
                }
                fragment = fragment3;
            } else { //default
                if (fragment4 == null) {
                    fragment4 = Fragment4.newInstance(something4);
                }
                fragment = fragment4;
            }
        }catch (Exception e) {
            Log.e(AppConstants.APP_TAG, "Cannot create fragment", e);
            finish();
        }

        return fragment;
    }

    @Override
    public int getCount() {
        int count = 4;
        return count;
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
}
Panos
  • 7,227
  • 13
  • 60
  • 95
  • I think this might have something to do with `FragmentStatePagerAdapter`s way of handling unused fragments. It destroys the fragment and removes it from the `FragmentManager`. Try using `FragmentPagerAdapter` instead and see how it goes. – JamMaster Nov 15 '15 at 01:28
  • 1
    Also i found this, seems like it could be of help if you really want to keep the resource friendly `FragmentStatePagerAdapter`. http://stackoverflow.com/questions/16960676/viewpager-in-tabfragment-not-loading-second-time – JamMaster Nov 15 '15 at 01:41
  • I tried the FragmentPagerAdapter , it seems to have helped but only a bit. Just a couple fragments worked, some didnt and after a couple of rotations even those dissapeared – Panos Nov 15 '15 at 01:41
  • @jammaster - Indeeed!!! I was using the simple getFragmentManager in the main fragment (victim of the copy paste)...!! Now with getChildFragmentManager() it seems to work fine. Please write it as an answer so I can accept it :) – Panos Nov 15 '15 at 01:49

1 Answers1

2

When instantiating the PagerAdapter, use getChildFragmentManager() instead of getFragmentManager(). If more info needed, similiar problem was solved here ViewPager in TabFragment not loading second time

Community
  • 1
  • 1
JamMaster
  • 1,446
  • 1
  • 14
  • 24