I am building an application with one Activity MainActivity
which consists of two fragments. And I came across with many tutorials that use FrameLayout
as the parent container for the fragment layout.
However, I came to know that I can still use LinearLayout
as parent container of the fragment and it works perfectly fine (so far).
My question, is there any side effect(s) of not using FrameLayout
as the parent container for the fragment layout?
If there isn't, then what is the advantage of using FrameLayout
over LinearLayout
(or other possible layout) as the parent container of the fragment layout.
A common rule of thumb when choosing layouts is to select the combination that results in the smallest number of nested layout views. Specific to your question, RelativeLayout is larger and more capable than the much simpler FrameLayout. So for simple layouts, the latter is probably more efficient.
So Linear Layout should preferred over Relative Layout! Also if you use weight_sum attribute within Linear Layout it would again “measure the child twice”. So avoid using this attribute as much as possible.
LinearLayout : is a ViewGroup that aligns all children in a single direction, vertically or horizontally. 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.
More complex layout but results are the same, flat Constraint Layout is slower than nested Linear Layout.
Fragments
themselves are rendered just like any View
, so you can use whatever parent ViewGroup
you would like depending on how you want your layout to look. This means there's no inherent benefit of LinearLayout
vs FrameLayout
tied to Fragments
at all.
The main difference between FrameLayout
and LinearLayout
is that Views
stack within a FrameLayout
.
In other words, if you want your Fragments
to potentially overlap, use a FrameLayout
. If you want them displayed linearly, use a LinearLayout
. If it's a fullscreen Fragment
then it likely doesn't matter which you choose.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With