Why use fragments?

I read the documentation and other topics on this topic, and I really don't feel confident; I do not see clear limits for using this technique.

Fragments are now considered best practice; Each action should be basically support for one or more fragments and not call the layout directly.

Fragments are created for:

  • allow Activity use a lot of fragments, change them among themselves, reuse these units ... ==> Fragment completely dependent on the Context activity, so if I need something in common that I can reuse and process in many actions, I I can create my own layouts or Views ... I donโ€™t like this additional level of complexity, which will be added by fragments.

  • better processing for different resolutions ==> OK for tablets / phones in the case of a long process, that we can show two (or more) fragments in the same activity on tablets and one after another on phones. But why use fragments always ?

  • handling callbacks to move between fragments (that is: if the user is registered, I will show the fragment again, I will show another fragment). ===> Just try to find out how many errors in the Facebook SDK Log-in because of this, in order to understand that this is really (?) ...

  • given that the Android application is action-based ... Adding other life cycles to the Activity would be better for developing the application ... I mean that modules, scripts, data management, and connectivity would be better designed this way. ===> This is the answer of someone who is used to seeing the Android SDK and Android Framework with the Snippets vision. I donโ€™t think itโ€™s wrong, but Iโ€™m not sure that it will give good results ... And it is really abstract ...

====> Why should I complicate my life by coding more, always using them? otherwise, why is it best practice if it is just a tool for some cases? what are these cases?

I'm sorry if I wrote too much and thanks for your time. I hope I draw your attention because I really need ideas and impressions about this topic.

Regards, Ahmed

+6
source share
3 answers

You do not always have to use fragments. Fragments have their own application, for example, when you want to place pages on the screen and exit it, or when you want to radically change the interface in different directions. When they make sense, use them. When they do not, skip them. I find that they make sense in about 10-20% of applications - I rarely see the need.

+2
source

If there is some positive aspect, besides simple reuse of logic using different layouts, this is the ability of the Fragments to be preserved by the system when the orientation changes, since while the Activity is restored from scratch, the Fragment can be saved and therefore their use is more stable than the activity. In addition, switching between fragments is faster.

Personally, if I donโ€™t need to bother with different orientations and layout sizes, I still prefer to use Fragments and a special container. Activity around it, stability and smooth switching between different screens.

+1
source

This is a fairly general question, not directly related to a specific programming problem. But, in my opinion, good software is based on good design and, therefore, on good understanding and good practice. So your question is good for stackoverflow.

So what about fragments. It took me a while to figure out why you can or should even use them. As @pskink said, you can live without them without any problems. But if you plan to deploy your software on different devices, you should definitely think about fragments.

Screen resolution and density are not the only issue. Think of a smartphone. The screen is much smaller, so you cannot present your application in the same way as on a tablet. For example, the flow of key parts. The left side is a list of elements, and when you click one element, you will see the details of this element on the right side. Easy to make on tablet. But on the smartphone, you put the master view in one fragment, and the detailed one in another.

You have two options for implementing this scenario. Or a program of different actions for smartphones and tablets, but because they actually do the same logic, it is better to use logic for fragments and reuse these fragments in two layouts (phone / tablet).

+1
source

Source: https://habr.com/ru/post/970737/


All Articles