I know that Activities
are designed to represent a single screen of my application, while Fragments
are designed to be reusable UI layouts with logic embedded inside of them.
Until not long ago, I developed an application as it said that they should be developed.
I created an Activity
to represent a screen of my application and used Fragments for ViewPager
or Google Maps
. I rarely created a ListFragment
or other UI that can be reused several times.
Recently I stumbled on a project that contains only 2 Activities
one is a SettingsActivity
and other one is the MainActivity
. The layout of the MainActivity
is populated with many hidden full screen UI fragments and only one is shown. In the Acitivty
logic there are many FragmentTransitions
between the different screens of the application.
What I liked about this approach is that because the application uses an ActionBar
, it stays intact and does not move with the screen switching animation, which is what happens with Activity
switching. This give a more fluent feel to those screen transitions.
So I guess what I'm asking is to share your current development manner regarding this topic, I know it might look like an opinion based question at first look but I look at it as an Android design and architecture question... Not really an opinion based one.
UPDATE (01.05.2014): Following this presentation by Eric Burke from Square, (which I have to say is a great presentation with a lot of useful tools for android developers. And I am not related in any way to Square)
http://www.infoq.com/presentations/Android-Design/
From my personal experience over the past few months, I found that the best way to construct my applications is to create groups of fragments that come to represent a flow in the application and present all those fragments in one Activity
. So basically you will have the same number of Activities
in your application as the number of flows.
That way the action bar stays intact on all the flow's screens, but is being recreated on changing a flow which makes a lot of sense. As Eric Burke states and as I have come to realize as well, the philosophy of using as few Activities
as possible is not applicable for all situations because it creates a mess in what he calls the "God" activity.
It depends what you want to build really. For example the
navigation drawer
uses fragments. Tabs usefragments
as well. Another good implementation,is where you have alistview
. When you rotate the phone and click a row the activity is shown in the remaining half of the screen. Personally,I usefragments
andfragment dialogs
,as it is more professional. Plus they are handled easier in rotation.Thing I did: Using less fragment when possible. Unfortunately, it's possible in almost case. So, I end up with a lot of fragments and a little of activities. Some drawbacks I've realized:
ActionBar
& Menu: When 2 fragment has different title, menu, thatwill hard to handle. Ex: when adding new fragment, you can change action bar title, but when pop it from
backstack
there is no way to restore the old title. You may need an Toolbar in every fragment for this case, but let believe me, that will spend you more time.startForResult
, activity has but fragment hasn't.My solution for this is using an Activity to wrap a fragment inside. So we have separate action bar, menu,
startActivityForResult
, animation,...You are free to use one of those.
Basically, you have to evaluate which is the best one to your app. Think about how you will manage the business flow and how to store/manage data preferences.
Think about, how Fragments store garbage data. When you implement the fragment, you have a activity root to fill with fragment(s). So, if your trying to implement a lot of activities with too much fragments, you have to consider performance on your app, coz you're manipulating (coarsely speaks) two context lifecycle, remember the complexity.
Remember: should I use fragments? Why shouldn't I?
regards.
Why I prefer Fragment over Activity in ALL CASES.
Activity is expensive. In Fragment, views and property states are separated - whenever a fragment is in
backstack
, its views will be destroyed. So you can stack much more Fragments than Activity.Backstack
manipulation. WithFragmentManager
, it's easy to clear all the Fragments, insert more than on Fragments and etcs. But for Activity, it will be a nightmare to manipulate those stuff.A much predictable lifecycle. As long as the host Activity is not recycled. the Fragments in the backstack will not be recycled. So it's possible to use
FragmentManager::getFragments()
to find specific Fragment (not encouraged).Well, according to Google's lectures (maybe here, I don't remember) , you should consider using Fragments whenever it's possible, as it makes your code easier to maintain and control.
However, I think that on some cases it can get too complex, as the activity that hosts the fragments need to navigate/communicate between them.
I think you should decide by yourself what's best for you. It's usually not that hard to convert an activity to a fragment and vice versa.
I've created a post about this dillema here, if you wish to read some further.
In my opinion it's not really relevant. The key factor to consider is
The main use of fragments is to build multipane activities, which makes it perfect for Tablet/Phone responsive apps.