3

I want to add a fragment as a child to another fragment
I am using ChildFragmentManager

Here is my ChildFragmentManagerActivity.java
ChildFragment.java
ParentFragment.java
whose layouts are as as follows
activity_childfragmentmanager.xml
layout_parentfragment.xml
layout_childfragment.xml

I was able to add the ParentFragment successfully. Please check below image
parent_fragment.png

But when i try to add the ChildFragment, it is appearing as follows
child_fragment

I want the child fragment to be added as a content by replacing the previous layout
Thanks in Advance

4
  • "I want the child fragment to be added as a content by replacing the previous layout." Can you explain it more clearly please? What is previous layout? Commented Jul 9, 2013 at 15:28
  • Hi koso Thanks for your comment. The red portion(layout_parentfragment) is the layout of ParentFragment. I am adding a child fragment to it. The blue portion(layout_childfragment) is the layout of ChildFragment. I want the blue to be replaced by red completely similar to a Fragment replace method. But it is just adding the ChildFragment instead of replacing it. Commented Jul 10, 2013 at 5:14
  • What about if you add frameLayout on the beginning of parent_layout with height=match_parent and visiblity=gone. And when you add your fragment to frameLayout, make that frameLayout visible, so it will cover whole parent fragment layout. I think you want to make that right? Commented Jul 10, 2013 at 5:40
  • Yes you are right! If i use FrameLayout and parent_layout height as match_parent it is working. But it is not working in 2 cases \n 1)** If layout height is set to "wrap_content". \n 2)If the parent layout is other than RelativeLayout or FrameLayout**.\n I want to solve the above two cases Commented Jul 11, 2013 at 6:19

1 Answer 1

5

A transaction doesn't remove the views that already exist in the container that will be used for the transaction. To remove those views you need to wrap the initial content of the ParentFragment as a fragment and replace that by the child fragment(with a replace transaction and not an add transaction). I've made some changes to your code, check it out below:

ParentFragment:

public class ParentFragment extends Fragment {

private static final int CONTAINER_ID = 0x2222;
private static final String INITIAL_FRAG = "initial_fragment";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    FrameLayout wrapper = new FrameLayout(getActivity());
    wrapper.setId(CONTAINER_ID);
    // look for our two possible fragments, if we don't find the
    // InitialContentFragment add it
    if (getChildFragmentManager().findFragmentByTag(INITIAL_FRAG) == null) {
        InitialContentFragment initContent = new InitialContentFragment();
        Bundle args = new Bundle();
        args.putString("text",
                "I'm the initial content fragment in the parent fragment");
        initContent.setArguments(args);
        getChildFragmentManager().beginTransaction()
                .add(CONTAINER_ID, initContent, INITIAL_FRAG).commit();
    }
    return wrapper;
}

public void requestFragmentTransaction() {
    FragmentTransaction fragmentTransaction = getChildFragmentManager()
            .beginTransaction();
    ChildFragment childFragment = new ChildFragment();
    Bundle args = new Bundle();
    args.putString("text", "Hi I am Child Fragment");
    childFragment.setArguments(args);
    fragmentTransaction.replace(CONTAINER_ID, childFragment, "ChildFragment");
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();

}

}

where the InitialContentFragment is:

public static class InitialContentFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // inflate the layout file that would normally be in the
        // ParentFragment at start
        View view = inflater.inflate(R.layout.layout_parentfragment,
                container, false);
        Bundle bundle = getArguments();
        final String text = bundle.getString("text");
        TextView textView = (TextView) view.findViewById(R.id.textView1);
        textView.setText(text);
        Button button = (Button) view.findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ParentFragment parent = (ParentFragment) InitialContentFragment.this
                        .getParentFragment();
                parent.requestFragmentTransaction();
            }
        });
        return view;
    }
}

As a side note, NEVER ignore the try-catch block like you did.

Sign up to request clarification or add additional context in comments.

4 Comments

Ive implemented your code for Navigation Drawer using the following code.stackoverflow.com/questions/23678513/… and its working absolutely fine, but the problem when you click on the navigation drawer list fragment. The Tab host tabs are appearing and working awesome, but when go back to another activity and come back to the tab host activity. The tabs are appearing, but the linear layout background of the tab host is appearing as shown in the images below of tabhost clicked single and twice. i.stack.imgur.com/CzIsC.png i.stack.imgur.com/Dmxn3.png
@B.rohitNare Please post a new question providing all of the details of your problem.
stackoverflow.com/questions/26483069/… @Luksprog Hey mate follow the link
@Luksprog, But it seems that in your solution, getChildFragmentManager().getBackStackEntryCount() always returns 0 ? any clue ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.