Android App with multiple views

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


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.


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.

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

    链接地址: http://www.djcxy.com/p/67998.html

    上一篇: 活动与视图

    下一篇: 具有多个视图的Android应用