115
votes

I often need the different parts of my applications to have their own special behavior and UI, and I don't know how fragments can help. In most cases, I think it is quicker to create 2 different activities (e.g., 1 for tablets and 1 for handsets), and to share the common behaviors and events in a third class.

So, keeping this in mind, why should I use fragments ?

5
whenever you want to have a lot of trouble and waste time basicallynikoss

5 Answers

78
votes

Fragments are more of a UI benefit in my opinion. It's convenient for the user sometimes to see two different views of two different classes on the same screen. If, in your moment of creativity, you decide it would be nice to display your application with, say, a listView that takes up half the screen and a webView that takes up the other half - so that when you click on a list item in fragment A it passes an intent to the webView in fragment B, and suddenly you see what you just clicked without the app switching activities - then you could use a fragment. That's just an example I came up with off the top of my head.

Bottom line: Fragments are two or more activities on the screen at the same time.

64
votes

The benefits I see when using fragments are:

  • Encapsulation of logic.
  • Better handle of the lifecycle of the fragment.
  • Reusable in other activities.

The drawbacks I see are:

  • More code(For example, instantiating a fragment manager, adding the fragment transaction, writing the callbacks of the fragment)
  • Communication between fragments and activities is harder. As @jonney said it, you would need to deal with a parcelable interface to serialize your objects you wish to pass.

So, when deciding to use a fragment, I would ask myself the following questions:

  • Is the lifecycle of the fragment different from the activity's lifecycle?

If the lifecycle is different, you get better handling of the lifecycle using a fragment. For example, if you want to destroy the fragment, but not the activity. Such is the case, when you have a pager adapter.

  • Is the fragment going to be used in several activities?

The user input events will be reusable if you use a fragment.

  • Is the amount of communication between the fragment and the activity small?

If you need to pass big objects to the fragment, you would need to deal with the code that serializes them. Also, if you need to communicate between fragment and activity, you would probably need to implement interfaces. This, in most cases, adds complexity to your codebase. It's not a difference maker, but a criteria to take into account.

21
votes

Google advises you to ALWAYS use Fragments.

Why? It's simple:

In the simplest case, Fragments are used like containers of activities.

Why do you need this? Again, it's simple.

Android 4 (ICS) supports both Smartphones and Tablets. This means the SAME application will be running on a smartphone and a tablet and they are likely to be very different.

Tablets have big screens which will be empty or unused - unless you assign it properly.

That means- Putting two fragments on one activity like Contact List and Contact Info.

The smatphone will display contact List, and on a touch- display the contact's Info.

On a tablet, the user will still see the list and the info will be next to it.

2 fragments- on one screen....

Smart? yes... supposed to be back compatible down to Android 1.6......


#############################################################

O.K, Already Knew That? then - just try to understand the case solved:

A lot of things work that way- list & details, Menus and Sub-Menus, Info, Detailed Info and some more detailed info. You want a way to keep it natural and smooth for a tablet which you expect to preform that way, but can't expect smartphone to display it all like the tablet did...

Get it?

for more Information, check out this. I really think you just need to catch the concept....

8
votes

Historically each screen in an Android app was implemented as a separate Activity. This creates a challenge in passing information between screens because the Android Intent mechanism does not allow passing a reference type (i.e. object) directly between Activities. Instead the object must be serialized or a globally accessible reference made available.

By making each screen a separate Fragment, this data passing headache is completely avoided. Fragments always exist within the context of a given Activity and can always access that Activity. By storing the information of interest within the Activity, the Fragment for each screen can simply access the object reference through the Activity.

https://softwareengineering.stackexchange.com/questions/244771/why-use-android-fragments

1
votes

Fragment primary support more dynamic & Large UI Screen like Tablet.Because Tablet screen is much larger than normal Handset. There is more room to combine & Interchange UI Component.

Fragment allow such design without the need for such complex change in the View hierarchy.

By divide activity layout in fragment, we become able to modify activity's appearance at runtime