Android App with multiple views - Best Practices?

后端 未结 3 888
猫巷女王i
猫巷女王i 2021-02-10 12:24

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 a

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-10 13:04

    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.

提交回复
热议问题