19

I haven't been able to find a way how to dynamically add fragment into existing dynamically added fragment. Do you know, if it is possible?

I am generating fragments this way:

FragmentManager fragMgr = getSupportFragmentManager();
FragmentTransaction xact = fragMgr.beginTransaction();

if(null == fragMgr.findFragmentByTag(FRAG1_TAG)) {
   xact.add(10101010, new DateTime(), FRAG1_TAG); 
}

if(null == fragMgr.findFragmentByTag(FRAG4_TAG)) {
   xact.add(7777, new loginForm(), FRAG4_TAG);

}

xact.commit(); 

How to add into FRAG4_TAG fragment another one?

Edit2:

I hard coded it's id to be able to work with it in future (where ll is my linearLayout in XML):

FrameLayout frml4 = (FrameLayout)inflater.inflate(R.layout.frame,null);
frml4.setId(7777);
frml4.setBackgroundColor(Color.YELLOW);

ll.addView(frml4);
Gili
  • 86,244
  • 97
  • 390
  • 689
Waypoint
  • 17,283
  • 39
  • 116
  • 170
  • 2
    did you tried? i don't think if it is possilbe fragment can't contain other fragment ... use Views for that(i face the same isuse ... i already have fragments lets call em LeftListView and DetailsPane now i wana have ViewPager in DetailsPane ... so i build fragment for each Pager page ... but it always ends with error on transaction commit ... after some research i found this http://stackoverflow.com/questions/5268361/fragments-in-action-bar-tab-fragments – Selvin Oct 27 '11 at 12:00
  • Yes I have tried, but it wasn't successfull. Thanks for comment – Waypoint Oct 27 '11 at 12:06
  • http://stackoverflow.com/questions/7700226/display-fragment-viewpager-within-a-fragment/7700305#7700305 , check if this solution works for you. it is a work around to insert use fragment inside another fragment. – Yashwanth Kumar Oct 27 '11 at 12:13

4 Answers4

32

I assume the problem that you are running into is that there is not an inflated view to add the fragment to because the original fragment, FRAG4_TAG, has not been inflated before you are trying to add it.

You can pass enough information to FRAG4_TAG in the Arguments to let it know that it should create and add a fragment (or what all fragments you need it to have) to itself during it's onCreateView, after the view has been inflated...

The layout for the activity...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="MyActivity"/>

    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/main_frag_container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
</LinearLayout>

The Activity...

public class MyActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        Fragment fragOne = new MyFragment();
        Bundle arguments = new Bundle();
        arguments.putBoolean("shouldYouCreateAChildFragment", true);
        fragOne.setArguments(arguments);
        ft.add(R.id.main_frag_container, fragOne);
        ft.commit();

    }
}

The layout for the fragment...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="20dp">
    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Some fragment"/>

    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/frag_container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
</LinearLayout>

The fragment...

public class MyFragment extends Fragment {
    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
        ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.frag_layout, container, false);

        boolean shouldCreateChild = getArguments().getBoolean("shouldYouCreateAChildFragment");

        if (shouldCreateChild) {
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();

            fm.beginTransaction();
            Fragment fragTwo = new MyFragment();
            Bundle arguments = new Bundle();
            arguments.putBoolean("shouldYouCreateAChildFragment", false);
            fragTwo.setArguments(arguments);
            ft.add(R.id.frag_container, fragTwo);
            ft.commit();

        }

        return layout;
    }
}

This example covers the case where you need to dynamically add fragments to a fragment that HAS NOT already been inflated and added to the hierarchy. Adding a fragment to a fragment that HAS already been inflated and added to the hierarchy is as simple as just specifying the target fragments container that you want to add to by ID like you would normally.

Kelly Merrell
  • 1,245
  • 1
  • 10
  • 16
3

As the documentation states "A fragment must always be embedded in an activity". So when you add a "sub-fragment" it will always belong to the activity even if you add it within your fragment class. For example if you later decide to remove the containing fragment the sub fragments won't be automatically removed. When I had to do the same I had to store in a vector the sub fragments and manually remove them in the onDestroy methods of my container fragment.

I think that fragments are not thought to be used like this

kingston
  • 11,053
  • 14
  • 62
  • 116
  • 1
    I fully agree with this. My example above *DOES NOT* handle the cleanup. You will have to do your own homework on that one. – Kelly Merrell Aug 26 '12 at 20:53
1

You cannot insert Fragments into other Fragments. (At least, not yet)

You can however replace Fragments with other Fragments with FragmentTransaction.replace(containerViewId, Fragment).

Dave
  • 6,064
  • 4
  • 31
  • 38
-2
@Override     
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);
    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.add(R.id.linear1, new activity()).commit();
}
lcnicolau
  • 3,252
  • 4
  • 36
  • 53