Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Single activity, multiple views

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

  1. I cannot pass object reference to activities’ constructors. In fact I found the mechanisms of startActivityForResultIntentonActivityResult 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.

  2. 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

like image 659
anonim Avatar asked Jun 02 '12 11:06

anonim


People also ask

Can one activity have multiple layouts?

Yes its possible. You can use as many layouts as possible for a single activity but obviously not simultaneously.

Why use single activity Android?

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.

How can I display one activity inside another Android?

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.


2 Answers

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.

like image 160
Fabian Knapp Avatar answered Nov 26 '22 22:11

Fabian Knapp


There is already an MVC framework built, PureMVC library.

like image 26
Juliana Avatar answered Nov 26 '22 22:11

Juliana