Shared publicly  - 
 
Has anyone experimented with creating a single-activity Android app? Meaning the entire app consists of fragments with a single activity.

Pros? Cons?

Also, what are your pro tips for deciding when to subdivide into activities versus fragments?
4
1
Luis de la Rosa's profile photoJulien Dodokal's profile photoRonald Ammann's profile photoHugo Visser's profile photo
11 comments
 
I've build a video game using one activity and a fragment per screen. This was mostly done in order to easily load the resources in memory during the splash and being able to keep the objects during the game. This also allows me to show some fragments on the canvas like a layer: UI elements during the game, game over score...
And this is very fast to switch between screens. https://play.google.com/store/apps/details?id=com.doodlegrub.android
Then I've build a lot of apps using several activities and fragments: sometimes it's just easier to use many activities (when there is a lot of listeners or if you use google maps which needs to in a separated activity).
 
MapView's tight coupling to MapActivity is an unfortunate limitation of Android. :-(

The point about multiple listeners is also important. The recommended way for a fragment to communicate back to its containing Activity is via an interface that the Activity implements. With many fragments, the Activity becomes a "god object" because it must implement so many interfaces.

In order to scale this approach, one would need to come up with an alternative to the listener interface idiom. Perhaps via an event bus.
 
+Eric Burke Take a look at OSM Droid instead of using MapView from google. You will have lots of options and not have to force yourself with MapActivity. Take a look at MyTopo MapViewer from us at Trimble Outdoors on the Google Play Store.
 
I'm putting as many fragments into an activity as possible. Usually I break it into a new activity when the requirements in the action bar are different. On the other hand in the group navigation mode in App Quarantine Pro I'm using only one activity and setting up the action bar for each fragment, like switching from tab to normal mode and such.
 
One more thing: in App Quarantine I have all the main screens that the user is using on his daily business in one activity. Further screens like settings or info screen are in separate activities. This way it's also easy to apply a DialogTheme on a tablet.
 
And yet another thing is that you possibly have more screen orientation issues the more fragments you're adding...
 
I agree with the God-Activity point you make +Eric Burke. Recently I started an app that quickly went messy because one Activity needed to support all of the fragments it could show. It made more sense to split up each separate, well...activity basically, into a new Activity which made the code much easier to understand and to maintain.
Generally when the app really switches context, I break it out to a new activity if that makes sense.

Also sometimes you'd still need multiple activities, for example if a screen is an entry point into your application.

I also found that calling back from an Fragment to an Activity will usually work fine, but the other way around is not the way it's intended ;) Fragments get recreated and referencing those fragments from an Activity will cause interesting bugs, although it will usually just crash.
 
I think that if you want to make an application that uses the ActionBar navigation tabs that coordinates with a ViewPager, then I think you will end up with at least one Activity that has many Fragments in it. If you don't have any details you can tap into which bring up an Activity, you'd end up with a single Activity. I think this is a consequence of a) not being able to nest Fragments within other Fragments and b) ViewPager working with Fragments.
Add a comment...