Martin Erlic
Martin Erlic

Reputation: 5665

Opening a new fragment from the onCreate() method of another

I'm trying to open a new fragment automatically from the onCreate() method of my original fragment. I found this code via How do i open a new fragment from another fragment. The only thing I'm confused about is the resource element. What does R.id.layout refer to in this particular case? What is the resource being replaced? It's asking for the containerViewId. What is that?

    TemplateFragment nextFrag = new TemplateFragment();
    this.getFragmentManager().beginTransaction()
            .replace(R.id.layout, nextFrag, TAG_FRAGMENT)
            .addToBackStack(null)
            .commit();

Upvotes: 1

Views: 159

Answers (2)

marcinj
marcinj

Reputation: 49976

This is an ID of the layout container where you fragment will be put, ie:

        <FrameLayout
            android:id="@+id/layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

which should exist in your activities layout xml.

in method javadoc you have:

* @param containerViewId Identifier of the container whose fragment(s) are
* to be replaced.

Upvotes: 3

Blackbelt
Blackbelt

Reputation: 157437

What does R.id.layout refer to in this particular case? What is the resource being replaced?

Fragments, when they want to display something, need a container where the result of their onCreateView will be inflated into. Of course this container should also be part of the activity currently at screen or you will not see anything

Upvotes: 2

Related Questions