How to get back to parent fragment?

Viewed 464

In parent fragment:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_parent, container, false);

        //child fragment
        FragmentManager childFragMan = getChildFragmentManager();
        FragmentTransaction childFragTrans = childFragMan.beginTransaction();
        ChildFragment childFragment = new ChildFragment ();
        childFragTrans.add(R.id.FRAGMENT_CHILD, childFragment);
        childFragTrans.commit();

Then in child fragment i need to get back to Parent fragment (hide child fragment) How to implement that ?

1 Answers
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_parent, container, false);

    if (savedInstanceState == null) {
        //child fragment
        FragmentManager childFragMan = getChildFragmentManager();
        FragmentTransaction childFragTrans = childFragMan.beginTransaction();
        ChildFragment childFragment = new ChildFragment ();
        childFragTrans.add(R.id.FRAGMENT_CHILD, childFragment, "childFragment");
        childFragTrans.commit();
    }

Then

FragmentManager childFragMan = getChildFragmentManager();
Fragment childFragment = childFragMan.findFragmentByTag("childFragment");
FragmentTransaction childFragTrans = childFragMan.beginTransaction();
childFragTrans.remove(childFragment);
childFragTrans.commit();
Related