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

KK_07k11A0585
  • 2,381
  • 3
  • 26
  • 33
  • "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? – Koso Jul 09 '13 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. – KK_07k11A0585 Jul 10 '13 at 05: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? – Koso Jul 10 '13 at 05: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 – KK_07k11A0585 Jul 11 '13 at 06:19

1 Answers1

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.

user
  • 86,916
  • 18
  • 197
  • 190
  • Ive implemented your code for Navigation Drawer using the following code.http://stackoverflow.com/questions/23678513/swapable-tabs-in-slider-menu-fragment 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.rohit Nare Oct 21 '14 at 08:32
  • @B.rohitNare Please post a new question providing all of the details of your problem. – user Oct 21 '14 at 08:49
  • http://stackoverflow.com/questions/26483069/navigation-drawer-with-tabhost-and-viewpager @Luksprog Hey mate follow the link – B.rohit Nare Oct 21 '14 at 09:17
  • @Luksprog, But it seems that in your solution, getChildFragmentManager().getBackStackEntryCount() always returns 0 ? any clue ? – Narendrasinh Dodiya Aug 26 '15 at 10:30