4

In an Android application, similar to an application market, I have a Home view and two other views contain lists. All these three lists will go to a list of products but with different arguments. And there is another view shows the product details view.

In such a scenario, when should I use Fragments and when should I use Activities? And how to determine? knowing that I search textbox in a tool bar which supposed to appear in the whole system.

The options I may have now is one activity in all the application contains the search toolbox. This activity will help in the navigation between fragments. The other option is to set the first 3 views in an activity, and the list of product in another activity, and the product details in a separated one.

Homam
  • 23,263
  • 32
  • 111
  • 187

1 Answers1

7

The reason that fragments were originally created was to allow for modular layout options on larger screen devices. The classic example is the dual pane layout that adapts based on your screen size.

dual pane layout

So the kinds of questions you might ask yourself to determine whether fragments are a good choice for you are:

  1. Do I want my app to adapt its look based on the screen size?
  2. Could I take advantage of a larger screen device to show 2, 3, 4 fragments on the same screen?
  3. Am I duplicating the same UI elements across all of my activities?

If yes, fragments are what you are looking for. If no you can probably use activities.

Fragments are certainly the future. In your example your base activity that contains all of your fragments could contain the search bar and could handle attaching new fragments and acting as the middle-man for dispatching data between fragments.

Error 454
  • 7,255
  • 2
  • 33
  • 48