Where to call getChildFragmentManager()?
Asked Answered
I

3

12

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 had (for example) TestFragment which looks like:

public class TestFragment extends Fragment {

    private View mFragmentView;
    private FrameLayout mFrameLayout;   

    public HistoryFragment() {
        super();
    }       

    @Override   
    public View onCreateView(LayoutInflater pInflater, ViewGroup pViewGroup, Bundle pBundle) {          

        mFragmentView = pInflater.inflate(R.layout.fragment_layout, pViewGroup, false);         

        mFrameLayout = (FrameLayout) mFragmentView.findViewById(R.id.framelayout);

        return mFragmentView;
    }   

    public void onDestroyView() {
        super.onDestroyView();
    }    

    /* ISSUE */
    public void doSomethingSpecial() {
        FragmentManager tFragmentManager = getChildFragmentManager();
    }
}

At the MainActivity I initialize my fragment like:

mTestFragment = new TestFragment();
mTestFragment.doSomethingSpecial();

Then I pass fragment to ViewPager via FragmentPagerAdapter.

Finally, I get an exception. But if I use only:

mTestFragment = new TestFragment();

Then it works.

Question

Where should I call getChildFragmentManager() method in my fragment? What I am doing wrong?

If you have a good example of using new Android Nested Fragments please share link with me. I would greatly appreciate for your help.

Independent answered 21/9, 2013 at 17:29 Comment(1)
Take a look at this link ... getChildFragment() on Programmatically added fragmentsMere
T
4

Short answer: I call and use getChildFragmentManager() in my Fragment's onViewCreated() method.

For example:

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    addInnerFragment(); // This method does the getChildFragmentManager() stuff.
}
Telles answered 9/6, 2016 at 13:36 Comment(0)
M
0

Actually I Just used in the following case.

I was trying to add a Map into a ViewPager. As you may know, the "pages" in the ViewPager are Fragments, so I was unable to use this code Sample using maps from an Activity. I was trying to get the SupportMapFragment from the Activity doing things like:

SupportMapFragment mapView = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);

But mapView was always null, so my application crashes.

I found the following answer and works inmediatly.

This can be an useful example where to use getChildFragmentManager()

Hope this can be helpful.

Milled answered 11/12, 2015 at 1:14 Comment(3)
Your first link is broken and the second link doesn't provide any insight on where to use getChildFragmentManager(). :-(Telles
That was a last year answer :/ You could edit the answer and provide feedback instead of down-votingMilled
I'm not sure what code the link contained, so couldn't really edit your answer. I felt the answer wasn't useful, hence the downvote - but at least I left a comment! :-)Telles
P
0

According to the docs, this method:

Return a private FragmentManager for placing and managing Fragments inside of this Fragment.

So you can use it if you want to add/replace a new fragment to your existing fragment, you should commit your TestFragment transaction first, then add the inner fragment to it. try something like this:

    TestFragment mTestFragment = new TestFragment();
//get the activity's fragment manager to add the fragment
    FragmentTransaction transaction= getFragmentManager().beginTransaction();
    transaction.add(R.id.container, mTestFragment).commit();
    //add the inner fragment
    mTestFragment.doSomethingSpecial();
Pliam answered 9/6, 2016 at 13:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.