Question

I am developing an Android app with fragments that has many possible flows. With simple Activity-based flow, it can flow like this:

A -> B -> E
A -> C -> E
A -> D -> E

With fragments, I would like to be able to do this same thing with a screen split into thirds. The question that I am asking is: in my layout XML file, how do I represent a space that may be filled with one of three different fragments?

Was it helpful?

Solution

Take a look at FragmentTransaction.add() function:

public abstract FragmentTransaction add (int containerViewId, ... )
...
containerViewId - Optional identifier of the container this fragment is to be placed in. If 0, it will not be placed in a container.

In other words, just create any empty GroupView in your xml layout and pass id of this group into this function when adding the fragment. Then remove it when you don't want it anymore.

OTHER TIPS

If you won't need to replace fragments in your activity, you can put the fragment declaration directly inside the XML.

<fragment
    android:name="com.mypackage.ui.MyFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/myfragment_Fragment">
</fragment>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top