Android: Single activity, multiple views

android, design-patterns

Solution

There are popular apps in market with only one or a few activities. They use fragments and switch them. I would prefer fragments over your approach. Although I think fragments are too complex for many purposes, for your use case it would be a good solution. Your UI behavior should be in fragments, and your activity is the controller part transferring data between your fragments. Fragments also have an own life cycle.

I don't like `startActivityForResult` either. If I have a set of activities - all providing data - and I don't know in which order they will be called, I prefer to use a singleton class then using intents for data transmission between activities. But you have to analyze your problem to get a good solution.

Problem

I’m not an Android pro though I’ve developed an app consisting of more than 50 activities which makes the app really large. After 8 weeks of developing, now there are problems which caused the app difficult to maintain and upgrade. Major ones I’m dealing with are I cannot pass object reference to activities’ constructors. In fact I found the mechanisms of `startActivityForResult` – `Intent` – `onActivityResult` really limiting and results in dirty code of many constants for actions per activity and a lot of `switch` `case` that is really hard to follow the flow of app. Another problem is that I do not know how to manage the life cycle of the whole app as each activity has its own life cycle. I had some successful experience with LWUIT and J2ME – polish which ignore J2ME MIDlets (similar to android activity) and implement their own architecture and windowing system with just one MIDlet as entry to the app. I’ve come up with the same idea for android. To clarify, I’m thinking of an app with just one main `Activity` and other activities implemented as objects which extend `View` object and these views can be dynamically added to main activity `FrameLayout` and stack on each other. Activities’ logic can be implemented in such classes and I've even found a way to implement dialogs in this way. Business and state objects can be passed to their constructor and it sounds good ignoring its side effect of writing a little more code. This way listeners can also be passed to views’ constructors which makes app UI switch and flow management easier. But the questions are: - Is it a good practice? - Wouldn't it lead me to performance or memory issues? I'm also aware of - Android: What is better - multiple activities or switching views manually? - Don't Overload a Single Activity Screen - Pattern one activity, multiple views. Advantages and disadvantages None of these clearly address the issues regarding performance or practice with reasonable evidence or documented reference Please someone help me with this

Original source

Related problems