Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to call getChildFragmentManager()?

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.

like image 401
AlexMomotov Avatar asked Sep 21 '13 17:09

AlexMomotov


People also ask

What is the use of getchildfragmentmanager () inside a fragment?

Fragments are also capable of hosting one or more child fragments. Inside a fragment, you can get a reference to the FragmentManager that manages the fragment's children through getChildFragmentManager () .

How do I use the fragmentmanager?

The appropriate FragmentManager property to reference depends on where the callsite is in the fragment hierarchy along with which fragment manager you are trying to access. Once you have a reference to the FragmentManager, you can use it to manipulate the fragments being displayed to the user.

How do I use myfragmentfactory with other fragment classes?

Other fragment classes are handled by the default behavior of FragmentFactory through super.instantiate (). You can then designate MyFragmentFactory as the factory to use when constructing your app's fragments by setting a property on the FragmentManager.

What is the difference between a host and a child fragment?

The host fragment in Example 1, hosts two child fragments that make up a split-view screen. The host fragment in Example 2, hosts a single child fragment that makes up the display fragment of a swipe view. Given this setup, you can think about each host as having a FragmentManager associated with it which manages its child fragments.


1 Answers

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.
}
like image 65
ban-geoengineering Avatar answered Oct 25 '22 03:10

ban-geoengineering