Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigating back to FragmentPagerAdapter -> fragments are empty

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?

like image 352
Amit Ishai Avatar asked Jul 16 '13 09:07

Amit Ishai


3 Answers

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.

like image 75
Mike Mitterer Avatar answered Nov 12 '22 11:11

Mike Mitterer


It sounds like you are using nested fragments since your ViewPager is inside a PagerFragment. Have you passed getChildFragmentManager() to the constructor of your FragmentPagerAdapter? If not you should.

I don't think you need a FragmentStatePagerAdapter, but I would give that a shot since it handles saving and restoring Fragment state. The fact that your onDestroyView() hack works makes me think that you may want a FragmentStatePagerAdapter.

It could also have something to do with the way the FragmentPagerAdapter adds Fragments. The FragmentPagerAdapter doesn't add Fragments to the backstack. Imagine if you had a 10+ pages added in your ViewPager and the user swiped through them. The user would need to hit back 11 times just to back out of the app.

It may also be related to this post: Nested Fragments and The Back Stack.

Also I'm not sure what you are adding the Fragment C to. Are you adding it to the same container as the ViewPager?

Well at least you have a few options to investigate. In these situations I like to debug down into the Android SDK source code and see what's causing the behaviour. I recommend grabbing the AOSP source and adding frameworks/support and frameworks/base as your SDK sources. That's the only true way to understand what is happening and avoid making random changes until things work.

like image 16
Pierre-Antoine LaFayette Avatar answered Nov 12 '22 10:11

Pierre-Antoine LaFayette


Use getChildFragmentManager() instead of getSupportFragmentManager(). It will work fine.

like image 4
vijay rathore Avatar answered Nov 12 '22 11:11

vijay rathore