Navigating back to FragmentPagerAdapter -> fragments are empty

Amit Ishai picture Amit Ishai · Jul 16, 2013 · Viewed 10k times · Source

I have a Fragment (I'll call it pagerFragment) that is added to the backstack and is visible. It holds a viewPager with a FragmentPagerAdapter. The FragmentPagerAdapter holds (let's say) two fragments: A and B.

First adding of the fragments works great.

Fragment A has a button that once clicked, adds a fragment (C) to the backstack.

The problem is this: if I add that fragment (C), and then click back, the pagerAdapter is empty, and I cannot see any fragments inside.

If I use a hack, and destroy the children fragments (A and B) in the pagerFragments onDestroyView(), this solves the problem, although I don't wan't to use this hack.

Any ideas what the issue could be?

Answer

Mike Mitterer picture Mike Mitterer · Sep 18, 2013

I had the same problem. The solution for me was simple:

in onCreateView I had:

// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getActivity()
    .getSupportFragmentManager());

where SectionPageAdapter is something like this:

class SectionsPagerAdapter extends FragmentPagerAdapter {
...
}

after changing getSupportFragmentManager to

mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());

it started working! Hope this helps.