One Activity and all other Fragments

Vineet Shukla picture Vineet Shukla · Aug 28, 2012 · Viewed 60.7k times · Source

I am thinking of implementing one screen with Activity and all other sreens with Fragments and managing all the fragments thru the activity.

Is it a good idea? and my answer is NO but still I want to know more clearly about this thought.

What are the pros and cons of the idea?

Note:

Please don't give me the link for fragment and activity.

EDIT:

Here is something over Fragments and activity:

Pros:

  1. Fragments are meant to be used with activities as a sub activity.
  2. Fragments are not the replacement for activities.
  3. Fragments are meant for reusability(Need to know in what way reusability can be achieved.).
  4. Fragments are the best way to write code to support both tablets and phones.

Cons:

  1. We need to implement the interface to get the data from fragments.
  2. For dialog we have to go a long way to show it.

Why should we use fragments if we are not considering tablets? What is the starting time difference between activity and fragment?

Answer

JustinMorris picture JustinMorris · Aug 31, 2012

It depends on the app you are creating. I've created several apps using both approaches and can't say one way is always better than the other. The latest app I created I used the single Activity approach and a Facebook style navigation. When selecting items from the navigation list I update a single Fragment container to display that section.

That said, having a single Activity also introduces a lot of complexities. Let's say you have an edit form, and for some of the items the user needs to select, or create, requires them to go to a new screen. With activities we'd just call the new screen with startActivityForResult but with Fragments there is no such thing so you end up storing the value on the Activity and having the main edit fragment check the Activity to see if data has been selected and should be displayed to the user.

What Aravind says about being stuck to a single Activity type is also true but not really that limiting. Your activity would be a FragmentActivity and as long as you don't need a MapView then there are no real limitations. If you do want to display maps though, it can be done, but you'll need to either modify the Android Compatibility Library to have FragmentActivity extend MapActivity or use the the publicly available android-support-v4-googlemaps.

Ultimately most the devs I know that went the one Activity route have gone back to multiple Activities to simplify their code. UI wise, on a tablet, you are some times stuck using a single Activity just to achieve what ever crazy interaction your designers come up with :)

-- EDIT --

Google has finally released MapFragment to the compatibility library so you no longer have to use the android-support-v4-googlemaps hack. Read about the update here: Google Maps Android API v2

-- EDIT 2 --

I just read this great post about the modern (2017) state of fragments and remembered this old answer. Thought I would share: Fragments: The Solution to All of Android's Problems