Warpzit
Warpzit

Reputation: 28152

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?

Upvotes: 10

Views: 482

Answers (3)

neteinstein
neteinstein

Reputation: 17613

I've been developing in Android since 1.5 so I have been developing from quite some time Activities and recently Fragments.

Quite frequently fragments left me with a sour taste in my mouth... an example was when I needed a kind of paginated Dashboard with buttons. For that I used a ViewPager + 1 fragment per button. I had all kind of problems because before Android 4.2 fragments couldn't be nested.

Another problem was the asynchronous mode of function of the fragments that when the needed to be moved from one place to the other quite rapidly it had all kind of misbehaviours.

Don't think that all was bad... in more simple cases, the use of fragments worked quite nicely.

So, in my opinion, whenever you have an area that is self-contained, that isn't moved frequently on the views, that can be reused in several screens and also you support tablets (or my in the future), use it.

If you need nested fragments, views that are re-arranged quite frequently, or code that will not be reused, don't.

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006779

An Activity is defined as: "An activity is a single, focused thing that the user can do"

That is more an issue of dated documentation than anything else. Activity has that same definition... when we are on a smaller screen size (e.g., phone). As you move up to larger screens, the odds of an activity being more complex than "a single, focused thing" increases.

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?

Here is my general heuristic:

  • If you anticipate that such-and-so piece of UI might exist standalone on a phone-sized screen, but be used in tandem with something else on a tablet-sized screen, make it a fragment.

  • If you anticipate that such-and-so piece of UI will always exist standalone, just create a simple activity.

  • If you anticipate that your ability to anticipate is not that good, err on the side of making more fragments. For example, you might say, "well, help will never need to be alongside anything else" and make it be an activity. Then, if you realize that other pieces of UI might benefit from the help being side-by-side with them rather than off on its own -- so the user can read the docs and perform the actions at the same time -- you will regret not having made help be a fragment, as you will have to do some re-work.

  • If such-and-so piece of UI would never exist standalone -- in other words, if it is more like a single widget than a full activity -- and you anticipate using it on multiple projects, make it be a single widget, in the form of a custom View or ViewGroup.

But, as jsmith indicates, there is no universal right or wrong answer. BTW, AFAIAC, jsmith's answer is the correct one here, but I was going to be way too wordy for a comment on his answer... :-)

Upvotes: 6

jsmith
jsmith

Reputation: 4897

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.

Upvotes: 14

Related Questions