5

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

MAC
  • 521
  • 1
  • 9
  • 21
  • Yes i did mean if it is better to have a seperate activity for each UI or to switch UIs in one activity. – MAC Jun 20 '10 at 23:00

3 Answers3

7

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.

chrisbunney
  • 5,819
  • 7
  • 48
  • 67
4

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.
Ryan Hayes
  • 5,290
  • 4
  • 42
  • 52
  • You can use intents to open activities directly where you know the specifics of the target activity and the user has no choice, such as when transitioning between private activities within an app – chrisbunney Jun 20 '10 at 18:12
  • Yea, that's what I meant, but I labeled it wrong. The "Open Directly" should be an intent where you specify the specific Component Name for the Activity you want to open, and Open Indirectly should be when you specify the category of Activity. – Ryan Hayes Jun 20 '10 at 18:37
  • @Ryan: this is a great answer (for which i gave +1) but i think that the question is whether to switch between UIs within one activity, or to have a separate activity for each UI. @MAC: could you clarify your question a bit? thanks. – mtmurdock Jun 20 '10 at 22:18
  • Yes i did mean if it is better to have a seperate activity for each UI or to switch between them. – MAC Jun 20 '10 at 23:00
  • @Chris: Yea, I reread the question and saw that a little later. Great answer, Chris – Ryan Hayes Jun 21 '10 at 13:43
1

While Intents may be a little extra work, I'd recommend using them, if you don't directly need to pass large blocks of data back and forth between the two.

If you just need to pass information TO each of the sub-programs, then you can easily do that with putExtra(String key, Bundle values);

By using intents, you spend a little time now in order to have a lot of flexibility later. You can start intents from different points, so you'd not need to write new code if one of your sub-applications wanted to start a different one, or you wanted a certain filetype opened with a file manager to open one of your sub-programs.

camperdave
  • 1,421
  • 1
  • 11
  • 16