How do I properly add multiple fragments to a fragment transition?

10,789

Solution 1

Unfortunately you cant use fragments in this way you need to use just one fragment per container. It will amount to the same visual layout anyway.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
    android:id="@+id/fragment_container1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</FrameLayout>
<FrameLayout
    android:id="@+id/fragment_container2"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>

and

FragmentManager manager = getFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
UrlListFragment urlfragment = new UrlListFragment();
MyWebFragment webfragment = new MyWebFragment();
trans.add(R.id.fragment_container1, urlfragment, "my_url_fragment");
trans.add(R.id.fragment_container2, webfragment, "my_web_fragment");
trans.commit();

Solution 2

To add multiple fragments inside one layout, you need to call add() and commit() methods for each individual fragment. But if you're adding your fragments in a loop, or for example like below:

FragmentManager manager = getFragmentManager();
manager.beginTransaction().add(R.id.parentView, myFragment1, "fragment:1").commit();
manager.beginTransaction().add(R.id.parentView, myFragment2, "fragment:2").commit();

This might also get you into some problems. Since, the commit() method doesn't add the fragment immediately, you need to make sure that the previous fragment was attached from activity's onAttachFragment() method and then move further adding another fragment.

Share:
10,789
Martin Osorio
Author by

Martin Osorio

Updated on June 12, 2022

Comments

  • Martin Osorio
    Martin Osorio over 1 year

    I recently asked a question about fragments here:

    After a lot of messing around I found what the problem was, but after more fooling around, and research (in which correct code seemed identical to mine), i cant figure out what my problem is.

    After everything is created, I find that only the last fragment added to the transaction is visible. This is my code to add them:

    FragmentManager manager = getFragmentManager();
    FragmentTransaction trans = manager.beginTransaction();
    UrlListFragment urlfragment = new UrlListFragment();
    MyWebFragment webfragment = new MyWebFragment();
    trans.add(R.id.fragment_container, urlfragment, "my_url_fragment");
    trans.add(R.id.fragment_container, webfragment, "my_web_fragment");
    trans.commit();
    

    And this is my main xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </FrameLayout>
    </LinearLayout>
    

    What am I doing wrong, or what can be done so both fragments are added correctly and can be seen correctly?

  • Ahmed Shendy
    Ahmed Shendy over 3 years
    if I used executePendingTransactions() for first commit of myFragment1, will it helps me in the problem you mentioned?