Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: when / why should I use FrameLayout instead of Fragment?

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?

like image 702
Terry Avatar asked Oct 18 '13 15:10

Terry


People also ask

Why do we prefer FrameLayout for fragments?

Main purpose of frame layout is to block the area required to fit the largest child view. If you use a Frame Layout as Fragment Container you can ensure that you always have the space available to accommodate the largest fragment layout.

When should we use FrameLayout in Android?

FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other.

What is the difference between fragment and FrameLayout?

Show activity on this post. A framelayout, Relative View and a few others represents a view in android and is extended from viewgroup. A Fragment is a an an Object that is used to represent a portion of a user interface and is usually hosted in an activity. A fragment has a viewgroup which you can assign an XML layout.

What is the difference between FrameLayout and RelativeLayout in Android?

RelativeLayout : is a ViewGroup that displays child views in relative positions. AbsoluteLayout : allows us to specify the exact location of the child views and widgets. TableLayout : is a view that groups its child views into rows and columns. FrameLayout : is a placeholder on screen that is used to display a single ...


1 Answers

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.

like image 125
Bryan Herbst Avatar answered Sep 25 '22 21:09

Bryan Herbst