Question

I am building a layout for large screens, that is supposed to consist of 2 different parts, a left one and a right one. For doing that I thought using 2 Fragments is the right choice.

Then I had a look on the example of the navigation with the Master/Detail-Flow. It has a 2-pane layout, where on the right is the navigation, and on the left is the detail view.

But in that example, different from what I expected to see, for the detail view there is a FrameLayout that then holds a Fragment, instead of a Fragment directly.

The layout XML looks like this (an example):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:baselineAligned="false"
    android:divider="?android:attr/dividerHorizontal"
    android:orientation="horizontal"
    android:showDividers="middle"
    tools:context=".WorkStationListActivity" >

    <fragment
        android:id="@+id/workstation_list"
        android:name="de.tuhh.ipmt.ialp.history.WorkStationListFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        tools:layout="@android:layout/list_content" />

    <FrameLayout
        android:id="@+id/workstation_detail_container"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3" />

</LinearLayout>

My question now is: why is a FrameLayout used instead of the Fragment itself for the detail view? What is the reason or the advantage? Should I use it too?

Was it helpful?

Solution

The detail container is a FrameLayout because the Fragment that is displayed will be replaced using FragmentTransaction's replace() method.

The first argument to replace() is the ID of container whose Fragments will be replaced. If the FrameLayout in this example were replaced with a Fragment, then both the WorkStationListFragment and whatever detail Fragment is currently shown would be replaced by the new Fragment. By encapsulating the Fragment within a FrameLayout, you can replace just the details.

OTHER TIPS

fragment tag: Can be used to immediately load fragment via XML, but cannot be replaced by transaction.replace() method. Can load fragment by name or class property.

FrameLayout tag: Can only load fragment via programmatically and also fragment can be replaced by transaction.replace() method. Can add/replace fragment by FragmentTransction.

FragmentContainerView tag: FragmentContainerView is the child of FrameLayout and most recommended view to load fragment, It supports properties of fragment tag: (name and class) so fragment can be loaded from XML, but unlike fragment tag, a fragment can be replaced by transaction.replace(). As we know it is a child of FrameLayout so supports all the features of FrameLayout, we can add fragment just like we did in the case of FrameLayout.

Also, an animation related issue of FrameLayout is resolved in FragmentCotainerView:

Previously, attempting to customize the enter and exit animations of Fragments caused an issue where the entering Fragment would be underneath the exiting Fragment until it completely exited the screen. This resulted in unpleasant -and erroneous- animations when transitioning between Fragments.

Check this link.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top