Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

So what are the exact advantages of Fragments in Android 3.0?

Tags:

Could somebody please explain the exact advantages of using Fragments? In what cases should we use Fragments?

For what I understand, this framework:

  • Helps reuse existing code - if I implement functionality in a Fragment, then it's relatively easy to display this fragment in multiple parts of my app, when the functionality is needed.

  • Helps dealing with multiple screen sizes - a device with a huge screen may be able to display multiple fragments at once, and for smaller devices I can display the fragments in separate Activities.

Is there more to this framework?

Edit:

I've been using Fragments quite extensively in a larger project for 3.0 tablets. To me the greatest advantage was that using fragments I could break up the logic that would go in a single, monolithic Activity into multiple, smaller Fragments. Big screens mean huge Activities, difficult to read, understand (especially to new team members), develop and maintain. Fragments certainly helped in this matter.

like image 413
Zsombor Erdődy-Nagy Avatar asked Apr 17 '11 18:04

Zsombor Erdődy-Nagy


People also ask

What is the advantage of using fragments?

Fragments allow such designs without the need for you to manage complex changes to the view hierarchy. By dividing the layout of an activity into fragments, you become able to modify the activity's appearance at runtime and preserve those changes in a back stack that's managed by the activity.

What is the use of Android fragments?

A Fragment represents a reusable portion of your app's UI. A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events. Fragments cannot live on their own--they must be hosted by an activity or another fragment.

What are the advantages of using fragment compared to activity?

Advantages of fragments include code reuse and modularity (e.g., using the same list view in many activities), including the ability to build multi-pane interfaces (mostly useful on tablets). The main disadvantage is (some) added complexity.

Are fragments still used in Android?

Using the support library, fragments are supported back to all relevant Android versions. Fragments encapsulate views and logic so that it is easier to reuse within activities.


1 Answers

Is there more to this framework?

  • Animated effects available when dynamically adding and removing fragments from the screen

  • Automatic BACK stack management, so the BACK button can remove dynamically-added fragments before eventually exiting the activity

  • Integration with the action bar for tabs, as a replacement for TabHost

  • Integration with the action bar for "list"-based navigation (really a Spinner in the action bar, toggling between different fragments)

  • Somewhat easier handling of configuration changes courtesy of setRetainInstance(true)

Also, responding to @Jim Blackler:

I share your confusion, since it's always been easy to customize Views which seems (to me) to solve all the same problems.

Everything offered by fragments can, by definition, be done using Views, simply because fragments are built on top of the view framework. However, fragments make more complicated scenarios involving dynamic fragments a bit easier IMHO.

Moreover, fragments in conjunction with the action bar seem quite likely to be a long-term area of attention for Google, meaning I expect a fair amount of additional work in this area over the next 2-3 releases.

like image 57
CommonsWare Avatar answered Jan 01 '23 20:01

CommonsWare