How and when to use fragments in applications?

I am highly doubtful about structuring my application. When to use Fragments is a big question for me. I understand the concept of a Fragment, but I would like to know how more experienced programmers use them. Is it only valid when there is a specific task that the fragment has to do? For example POP-UP, which layout is better to use? And back to snippets, usually you have a skelleton fragment that can be used for more than one and then change it to different final shapes (small changes) or will you just use different snippets if the layout changes?

+3


source to share


2 answers


You should use snippets in several situations, but it really depends on your application.

If you have a layout to be used in multiple places and the code is relatively the same, this is a very good candidate for a snippet, so you can leave the code below.



If you are building an application in which the layout changes depending on the orientation or type of device (tablet with phone), then fragments are highly recommended to keep different layouts. This makes it easier to change or display multiple layouts if needed on a tablet.

I'm sure there are more scenarios out there, but I'd say these are the basics for solving fragments.

+2


source


The most common way I use them is as a kind of plugin for my actions. Let's say I have several activities and they all need to display an address, a phone number and some buttons (call number, start navigation / gps, etc.). I pass in a fragment id and the fragment does the work of grabbing information and filling in the fields for my actions. This way, if I need to add something, tell me the email, I just need to change one section of code, not every action and layout file.



+1


source







All Articles