0

When I read tutorials about supporting different screen sizes for Android, they often use FrameLayout as the placeholder for the fragment in FragmentTransaction uses. Like this:

getSupportFragmentMananager().beginTransaction()
.replace(R.id.frame_layout, fragment)

So why should we use FrameLayout ? Are there any advantages or disadvantages when using FrameLayout as the fragment container ? (I have tried RelativeLayout as the fragment container instead and it's still working well). I am a newbie so I hope will get a clear and thorough answer.

Edit: I have read the answers in the question link that you provided (the possible duplicate). However, it lacks clear example. I really want a simple example that demonstrate why I should use FrameLayout instead of other layout types.

Dan
  • 45
  • 10
  • I see it. Thank you Zeero0 – Dan Jan 01 '18 at 02:50
  • Thanks Zeroo0. I have read the answers in that question but it lacks clear example. Can anyone explainn it in an more understandable way ? – Dan Jan 01 '18 at 03:11
  • becuase fragments are stacked, which is in same nature as frame layouts which stack its children – Remario Jan 01 '18 at 03:17
  • `I really want a simple example that demonstrate why I should use FrameLayout instead of other layout types.` Try it yourself and **learn the different usage of each layout** with or without Fragment. Just ask yourself like : *why should I just use LinearLayout if ConstrainsLayout can do much more?* – Enzokie Jan 01 '18 at 03:27
  • Final note: If you are just going to use `replace` then `FrameLayout` is not compulsory, so use any Layout that you think make sense! – Enzokie Jan 01 '18 at 03:36

1 Answers1

2

To supplement the previous answer, the real asnwer is this, when you use a fragment transaction, you normally want to add a fragment to a saved spot in a layout for display. You basically wish to see this one view displayed, the reason why FrameLayout is the normative preference, is directly linked to the design purpose. Put simply all other layouts are mainly used for multi child inclusion. FrameLayout handles a single child more efficiently since there is no particular ordering, beside from gravity assignment.

Remario
  • 3,813
  • 2
  • 18
  • 25
  • there is no need for examples, its really a design choice , architectual stand point in laying out items. FrameLayout normally is one child, vs Linear and relative where several child layouts are composed on positions and orientation. – Remario Jan 01 '18 at 03:31
  • and also, frame layout provides switching, because items are stacked, each receives a position, which you can manipulate, again because framelayout is not orientation or position based on locations across the layout – Remario Jan 01 '18 at 03:35
  • The OP's example uses `replace` so we can say we only have 1 child Fragment. – Enzokie Jan 01 '18 at 03:37
  • exactly the very fact you are using repplace, which means remove any existing item and add again, means one item. Hence a frame layout – Remario Jan 01 '18 at 03:40
  • Not really, mostly any layout can handle that. – Enzokie Jan 01 '18 at 03:41
  • Yes thats a fact, remember every layout is somewhat specialized, a linear layout can emaulate a relative layout, but common sense dictates if you want more variability a relative layout is suffice. Going further if you want more control to reduce layout size, a contraint layout is best. Framelayout to model a relativelayout would be too complex, hence a single item is best. Make sense? – Remario Jan 01 '18 at 03:43
  • You are free to disagree since that is your design choice but the docs never explicitly mentioned that FrameLayout is the only options. I agree that Layout is specialize on some case but ask yourself how many advantage you gain from using FrameLayout over the Layout with this small case? does it take you more than 30mins to write such code if you don't use FrameLayout? – Enzokie Jan 01 '18 at 03:51
  • whats your design choice for this question? – Remario Jan 01 '18 at 03:56
  • Sorry I won't bite such vague question since it will just open a different kind of argument. Lets only focus on the FrameLayout topic. – Enzokie Jan 01 '18 at 04:06
  • Thanks Remario. This is really the answer that I expected :D thank you so much (y) – Dan Jan 01 '18 at 04:22