Going to fragments FragmentPagerAdapter & # 8594; empty

I have a fragment (I will call it pagerFragment ) that will be added to the stack and visible. It contains a viewPager with a FragmentPagerAdapter . FragmentPagerAdapter contains (say) two fragments: A and B.

The first addition of fragments works fine.

Fragment A has a button that once pressed adds fragment (C) to the stack back.

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

If I use hack and destroy pieces of children (A and B) in pagerFragment onDestroyView() , this solves the problem, although I do not want to use this hack.

What ideas might come up?

+48
android android-fragments android-viewpager fragmentpageradapter back-stack
Jul 16 '13 at 9:27
source share
4 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 the SectionPageAdapter looks something like this:

 class SectionsPagerAdapter extends FragmentPagerAdapter { ... } 

after changing getSupportFragmentManager to

 mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager()); 

he started to work! Hope this helps.

+74
Sep 18 '13 at 13:21
source share

It looks 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 do not think you need a FragmentStatePagerAdapter, but I would give this snapshot, since it handles saving and restoring the state of the fragment. The fact that your onDestroyView () workflow makes me think you might need a FragmentStatePagerAdapter.

It may also have something to do with how the FragmentPagerAdapter adds fragments. FragmentPagerAdapter does not add fragments to the stack. Imagine that more than 10 pages have been added to your ViewPager, and the user has skipped through them. The user will need to hit 11 times to return from the application.

It may also be associated with this post: Nested Snippets and Back Stack .

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

Well, at least you have a few options for research. In these situations, I like to debug the source code of the Android SDK and see what causes the behavior. I recommend grabbing the AOSP source and adding frameworks / support and frameworks / base as SDK sources. This is the only true way to understand what is happening and to avoid accidental changes until everything works.

+11
Aug 22 '13 at 15:46
source share

I just ran into a problem in our project. The main reason is how the FragmentPagerAdapter works:

The FragmentPagerAdapter simply separates the fragment that is not currently needed from its view, but does not remove it from the FragmentManager. When he wants to display the fragment again, he will see if the FragmentManager saves the Fragment using a tag that is created from the ViewPager view identifier and the identifier returned by calling the adapter's getItemId (position) call. If he finds a fragment, he simply plans to bind the fragment to its representation in the update transaction of the FragmentManager. Only if he does not find the fragment in this way, he creates a new one using the adapter call getItem (position)!

The problem with the fragment containing the ViewPager with the FragmentPagerAdapter is that the contents of the FragmentManager are never cleared when the contained fragment is pushed onto the back stack. If the containing fragment returns from the back stack, it creates a new View, but the FragmentManager still contains the fragments that were attached to the old view, and the attachment of the existing fragment no longer works.

The easiest way to get rid of this problem is to avoid nested fragments. :)

The second easiest way, as mentioned in other posts, is to use the ChildFragmentManager for the FragmentPagerAdapter, as it updates correctly during the life cycle of the container fragment.

As there are projects (like my current ones) where both options are impossible, I published a solution here that works with an arbitrary FragmentManager using hashCode sub-fragments as the identifier of the fragment element in this position.This is due to the price of storing all fragments for all positions in the adapter.

 public class MyPagerAdapter extends FragmentPagerAdapter { private static int COUNT = ...; private final FragmentManager fragmentManager; private Fragment[] subFragments = new Fragment[COUNT]; private FragmentTransaction cleanupTransaction; public MyPagerAdapter(FragmentManager fragmentManager) { super(fragmentManager); this.fragmentManager = fragmentManager; } @Override public Fragment getItem(int position) { return getSubFragmentAtPosition(position); } @Override public int getCount() { return COUNT; } @Override public long getItemId(int position) { return getSubFragmentAtPosition(position).hashCode(); } //The next three methods are needed to remove fragments no longer used from the fragment manager @Override public void startUpdate(ViewGroup container) { super.startUpdate(container); cleanupTransaction = fragmentManager.beginTransaction(); } @Override public void destroyItem(ViewGroup container, int position, Object object) { super.destroyItem(container, position, object); cleanupTransaction.remove((Fragment) object); } @Override public void finishUpdate(ViewGroup container) { super.finishUpdate(container); cleanupTransaction.commit(); } private Fragment getSubFragmentAtPosition(int position){ if (subFragments[position] == null){ subFragments[position] = ...; } return subFragments[position]; } 

}

0
Nov 21 '17 at 10:49 on
source share

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

0
Dec 15 '17 at 13:26
source share



All Articles