Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android App with multiple views - Best Practices?

Tags:

android

I am new to developing for android. I have a question regarding some best practices. My app is like a dashboard from which multiple different "sub-activities" can be started and done.

I am wondering what is the best way to structure the app. One way is to have different layouts and load and unload them appropriately. The other is to start new activities using intents. At least this is what i have gathered from what i have read.

What in your opinion is the best way to go.

Thanks

like image 225
MAC Avatar asked Jun 20 '10 17:06

MAC


2 Answers

I've found in my applications that each Activity is generally responsible for a single UI view.

So rather than loading and unloading different layouts, which can potentially get quite messy, it is better to separate each sub-activity into its own Activity class and use explicit intents (intents that name the target activity explicitly rather than relying on an intent filter) to move between them.

like image 143
chrisbunney Avatar answered Oct 14 '22 06:10

chrisbunney


The decision you have to make is whether or not your activities should be tightly or loosely coupled. Loading and unloading the activity is typically appropriate from within your own app. Using intents is appropriate when you need to open an activity that you may or may not know the specifics of. For example, you would open another activity from your main menu (assuming you have one) directly. Then later, let's say you need to open up an address with a map, you would use an intent, because you don't really know the SPECIFIC activity to open. Secondly, using intents are best for when there are multiple activities that could do the same function, such as opening a URL in a browser.

So in summary:

Open Directly (Loading a new view or using Intent specifying the Component Name)

  • Tightly coupled
  • Know specifics of the Activity to load

Open Indirectly (Intent specifying the category of Activities that can handle it)

  • Don't necessarily know the specifics of the Activity beyond that it can perform some action that has been advertised.
  • There are multiple Activities that can perform the desired action, and you want the user to be able to choose for themselves which Activity to use.
like image 44
Ryan Hayes Avatar answered Oct 14 '22 07:10

Ryan Hayes