Search code examples
androidfragmentmethodology

How and when to use Fragments in applications?


I am very in doubt of the structuring of my application. When to use Fragments is a big question for me still. I understand the concept of fragment, but I would like to know how more experienced programmers use them. Is it really only when there is a specific task the fragment should do? For instance, POP-UPs, what layout would be better used? And back to the fragments, do you usually have a skelleton fragment that can be used for more than one thing, and then reshape it to different final forms (minor changes) or would you just use different fragments if the layout changes?


Solution

  • You should use fragments in a few situations but its going to depend on your app really.

    If you have a layout that is going to be used in multiple places and the code is relatively the same, that is a very good candidate for a fragment so you can keep code down.

    If you are making an app where the layout changes based on orientation or device type (tablet vs phone) then fragments are highly recommended to hold different layouts. It makes it easier to change or show multiple layouts if need be on a tablet.

    I'm sure there are more scenarios but I would say these are the basics for fragment decision