Is it fine to use LinearLayout instead of FrameLayout?

fruqi picture fruqi · Dec 31, 2015 · Viewed 9.9k times · Source

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.

Answer

trevor-e picture trevor-e · Dec 31, 2015

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.