Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically add fragment into fragment

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);
like image 938
Waypoint Avatar asked Oct 27 '11 11:10

Waypoint


People also ask

Can I add fragment to fragment?

You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.

Can we add or remove fragments dynamically within an activity?

The FragmentManager class allows you to add, remove and replace fragments in the layout of your activity. It can accessed in an activity via the getFragmentManager() method. The modifications must be performed in a transaction via the FragmentTransaction class.

How do you fragment a fragment?

Adding and removing fragments. To add a fragment to a FragmentManager , call add() on the transaction. This method receives the ID of the container for the fragment, as well as the class name of the fragment you wish to add. The added fragment is moved to the RESUMED state.


Video Answer


1 Answers

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.

like image 78
Kelly Merrell Avatar answered Oct 20 '22 17:10

Kelly Merrell