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:
I'm also aware of
None of these clearly address the issues regarding performance or practice with reasonable evidence or documented reference
Please someone help me with this
Yes its possible. You can use as many layouts as possible for a single activity but obviously not simultaneously.
Instead of using multiple activities for login and registration, we can simply approach this architecture by creating fragments for every module for authentication and then bound them in a Single activity. It also provides the mobility to switch between the fragments and increase the stability of the application.
You can use a Fragment API to complete the task. See full details in developer's guide. Then create a MyFragment class and load it when appropriate.
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.
There is already an MVC framework built, PureMVC library.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With