ViewPager with Nested Fragments?

AlexMomotov picture AlexMomotov · Sep 24, 2013 · Viewed 14.7k times · Source

My problem

According to the Google's docs:

You can now embed fragments inside fragments. This is useful for a variety of situations in which you want to place dynamic and re-usable UI components into a UI component that is itself dynamic and re-usable. For example, if you use ViewPager to create fragments that swipe left and right and consume a majority of the screen space, you can now insert fragments into each fragment page. To nest a fragment, simply call getChildFragmentManager() on the Fragment in which you want to add a fragment. This returns a FragmentManager that you can use like you normally do from the top-level activity to create fragment transactions. For example, here’s some code that adds a fragment from within an existing Fragment class:

Fragment videoFragment = new VideoPlayerFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.video_fragment, videoFragment).commit();

So I created my own PageFragment. And my PageFragmentAdapterconsist of 3 pages of PageFragment. I also have six fragments: Fragment1, Fragment2, Fragment3, FragmentA, FragmentB, FragmentC. In my MainActivity I initialize my six fragments, my ViewPager and its PageFragmentAdapter (which initialize three PageFragment for itself).

Then I use this method to attach my Fragment1, Fragment2, Fragment3 to each page (to each PageFragment) of PageFragmentAdapter:

PageFragmentAdapter tPageFragmentAdapter = new PageFragmentAdapter(getSupportFragmentManager());
tPageFragmentAdapter.setFirstPage(tFragment1);
tPageFragmentAdapter.setSecondPage(tFragment2);
tPageFragmentAdapter.setThirdPage(tFragment3);

I can recive (via methods in PageFragmentAdapter) Fragments and make "nested fragment" into my PageFragment (of course in onCreate()) like:

getChildFragmentManager().beginTransaction()
.add(R.id.framelayout_history, mFragment)
.addToBackStack(null)
.commit();

Note: I use PageFragment.setNestedFragment() to set mFragment in it.

And it works great! It will help me (but this story is not about it) to simply replace fragments in ViewPager like:

tPageFragmentAdapter.replaceFirstPage(tFragmentA);

But I have one huge problem. Again Google's docs:

Implementation of PagerAdapter that represents each page as a Fragment that is persistently kept in the fragment manager as long as the user can return to the page. This version of the pager is best for use when there are a handful of typically more static fragments to be paged through, such as a set of tabs. The fragment of each page the user visits will be kept in memory, though its view hierarchy may be destroyed when not visible. This can result in using a significant amount of memory since fragment instances can hold on to an arbitrary amount of state.

As you can see PageFragmentAdapter can destroy each my PageFragment and of course its nested fragment. And when it will be recreated it will have a NULL instead mFragment value. I am trying to save it via getChildFragmentManager().putFragment() and then get it by getChildFragmentManager().getFragment(). But it doesn't worked.

Question

So my question is: How to save my nested fragment in my parent fragment? Is it possible?

I would greatly appreciate for your help. Alex. P.S. Really sorry for my English:)

EDIT

Thx @AndroidBegin.com for the answer!

This seems to be a bug in the newly added support for nested fragments. Basically, the child FragmentManager ends up with a broken internal state when it is detached from the activity. A short-term workaround that fixed it for me is to add the following to onDetach() of every Fragment which you call getChildFragmentManager() on:

@Override
public void onDetach() {
    super.onDetach();

    try {
        Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
        childFragmentManager.setAccessible(true);
        childFragmentManager.set(this, null);

    } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}

Answer

AndroidBegin.com picture AndroidBegin.com · Sep 24, 2013

Try this on your fragment.

public class Fragment2 extends SherlockFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.viewpager_main, container, false);
        // Locate the ViewPager in viewpager_main.xml
        ViewPager mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
        // Set the ViewPagerAdapter into ViewPager
        mViewPager.setAdapter(new ViewPagerAdapter(getChildFragmentManager()));
        return view;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        try {
            Field childFragmentManager = Fragment.class
                    .getDeclaredField("mChildFragmentManager");
            childFragmentManager.setAccessible(true);
            childFragmentManager.set(this, null);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}

Source: https://www.swipetips.com/actionbarsherlock-side-menu-navigation-nested-viewpager-fragment-tabs-tutorial/