kvel
kvel

Reputation: 497

Android: how to use FragmentTransaction.add in a fragment

So I am trying to add a fragment within a fragment, so I was told to change my Fragment Transaction variable to go from:

FragmentTransaction ft = fm.beginTransaction();

to

FragmentTransaction ft = getChildFragmentManager().beginTransaction();

However, I am not sure what Id I would use when I call ft.add();

previously I was using android.R.id.content. Is there a similar id I can use for a fragment, or which Id should I use to be able to add a new fragment from within my fragment.

Edit:

activity_mainfrag:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#EEEEEE"
    android:orientation="vertical">

    ...

</LinearLayout>

Upvotes: 0

Views: 292

Answers (1)

raktale
raktale

Reputation: 465

Use this to add fragment:

    FragmentTransaction ft = getChildFragmentManager().beginTransaction();
    ft.replace(R.id.container_parent, fragment, fragment.getTagText());
    ft.addToBackStack(fragment.getTagText());
    ft.commit();

Upvotes: 1

Related Questions