1

Up until recently, I've been using Activities for most of my Android apps. While diving into Fragments, it seems that Fragments are meant for larger screens like tablets and less so for smartphones because Fragments are mostly used to add views dynamically to parts of the UI when it is needed. Is this true or should Fragments be used exclusively for smartphones now and activities should be avoided? What exactly is the major benefit of using fragments over activities when it comes to smartphones?

AndroidDev
  • 20,063
  • 26
  • 131
  • 216
  • 2
    You can checkout [this](http://andytsui.wordpress.com/2011/10/23/to-fragment-or-not-to-fragment/), [this](http://stackoverflow.com/questions/20306091/dilemma-when-to-use-fragments-vs-activities) and also [this](http://stackoverflow.com/questions/10478233/android-need-some-clarifications-of-fragments-vs-activities-and-views). – MysticMagicϡ Jun 13 '14 at 06:10
  • you should use fragments. You are encouraged to use fragments. It depends on the effective utilization of the screen space to display the ui. – Raghunandan Jun 13 '14 at 06:11
  • 1
    Fragments aren't "meant for larger screens", they're for making your UI logic to remain likely the same and work across a variety of screen sizes. In contrast, not using a Fragment means your code will *only* work on a specific rough screen size, or else require a bunch of duplicated logic. – Tobias Jun 13 '14 at 06:12
  • @Tobias So are you effectively saying that you really never need to use activites anymore? – AndroidDev Jun 13 '14 at 06:45

3 Answers3

2

There are a lot of benefits to using Fragments. Aside from the obvious reason of providing flexible views for different screen sizes, Fragments can also be used to apply a certain layout to an overall Activity or have pretty similar views that carry separate logic.

For instance, when implementing a Navigational Drawer, the layout for the drawer is associated with the Main Activity and its layout, but you can use fragments to switch pages and do other various tasks in the app. When switching pages, you can replace a specific view in your main layout and thus you would have different views, but the navigational drawer layout will still be accessible to all of the views.

This is just one example, but Fragments are very useful and they are actually recommended.

Useful Links:

Fragments

Use cases

Similar Question

Nav Drawer Example

Community
  • 1
  • 1
Shirwa Mohamed
  • 191
  • 1
  • 9
  • So are you saying that there really is no need to use activities anymore? – AndroidDev Jun 13 '14 at 06:43
  • no not at all! Activities have their uses too, but Fragments are easy to reuse and more flexible than Activities. If you can, I would use Fragments, use Activities only when you feel like you have to. Fragments also provide the ability to commit to the backstack which is very useful. – Shirwa Mohamed Jun 13 '14 at 06:47
0

Fragments make it easy to reuse components in different layouts, e.g., you can build single-pane layouts for handsets (phones) and multi-pane layouts for tablets. This is not limited to tablets; for example, you can use fragments also to support different layout for landscape and portrait orientation on a smartphone.

0

As far as I know , It depends on the conditions. Benefits of Fragments:

  1. When the time there is no fragments, many big company has implemented many kinds of

fragment-like "UI systems " to avoid using many activities .

Some company used android View to divide the too much logic of a activity , some company

used android window so that it can be very faster when switching windows.

For example , UCBrowser (UC.cn) is a single activity ,or Email from google.

  1. Too many Activities means many consumptions. Communications between activities are complex and consume large ,too.

  2. Fragment is a very wonderful tool that to build complex UI easiliy, such as tabhost , or viewpager , or slidingmenu. I dont think it was born for tablet, it was born for mobile developing! (We can say googbye to AcitivtyGroup)

  3. However, activities is very important, too. We should decide which pages should be used fragment which should be used Activity.

5.Fragments are easy to reuse components in different activities , you can embed them in different activities.

My principle is:

If functions of some pages was very simple, and they was "belong to" a Activity but cant share Screen with the Activity , I adopt Fragment. Such as a simple sub-Setting UI that was belong a SettingActivity

If some pages was embed in a large container, such as tabhost , I adopt Fragment. If some

If the function of a page is very independent, I adopt Activity, such as "AboutActivity".

All in All, Fragment or Activity, We adopt one of them by the function of the pages.

Alex
  • 41
  • 5