Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To fragment or not to fragment?

As I've started to adopt Fragments more and better but also as Fragments functionality is increased (Fragments in Fragments, MapFragments) I'm starting to reach a point where I need to define when I should make a new View/Action as a Fragment or as an Activity?

An Activity is defined as:

An activity is a single, focused thing that the user can do.

But a Fragments have kinda taken that definition instead as described in the docs:

For example, a news application can use one fragment to show a list of articles on the left and another fragment to display an article on the right—both fragments appear in one activity

This is two things the user can do in one Activity with two Fragments.

So I'd like some input/help to figure out what is the best approach to decide if I should make a new action/view as a Fragment or as an Activity?

like image 879
Warpzit Avatar asked Dec 13 '12 17:12

Warpzit


1 Answers

The answer depends on you and your development practices (or those of your company). However, my opinion is this: At a minimum, if you think the functionality being developed could be used within multiple Activities, or if it could ever be used in an Activity alongside another view (as on a tablet), then you should make it a Fragment.

We've recently adopted the philosophy of creating Fragments in all cases. Our Activities are now just top level coordinators, basically the glue that brings things together. This makes for a consistent and flexible architecture. This is important to us as we have numerous engineers at a couple of locations working on code.

like image 132
jsmith Avatar answered Sep 19 '22 05:09

jsmith