Android 如何获得多个图标以在一个应用程序中启动不同的活动?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3270409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get multiple icons to launch different activities in one application?
提问by afonseca
I have an application with two activities and I'd like to be able to have two icons appear in the launcher, each launching the respective activity within the app.
我有一个包含两个活动的应用程序,我希望能够在启动器中显示两个图标,每个图标都在应用程序中启动相应的活动。
Specifically, I want one icon to launch my main app, and another icon to launch my settings activity. Is this possible?
具体来说,我想要一个图标来启动我的主应用程序,另一个图标来启动我的设置活动。这可能吗?
Here is what I've tried so far:
这是我迄今为止尝试过的:
<activity android:label="MyApp" android:name=".MyApp">
<intent-filter>
<action android:name=".MyApp"/>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:label="Settings" android:name=".Settings">
<intent-filter>
<action android:name=".Settings"/>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
This creates two launcher icons, but they both run my main app instead of the second icon running my settings app. I've tried just having the launcher category but then I don't get an icon so it looks like I need the main action as well.
这会创建两个启动器图标,但它们都运行我的主应用程序,而不是运行我的设置应用程序的第二个图标。我试过只使用启动器类别,但后来我没有得到图标,所以看起来我也需要主要操作。
Is this the right approach or should I be declaring two applications in the manifest instead?
这是正确的方法还是我应该在清单中声明两个应用程序?
回答by Rich Schuler
What you need to do is have your settings activity launch in another task. You can do this by specifying its task affinity. This is done with the attribute android:taskAffinity
. By default all activities share the same task affinity that defaults to main package specified in the manifest. On your settings activity you can specify android:taskAffinity="your.own.package.SettingsTask"
to have the settings activity launch in its own task.
您需要做的是在另一个任务中启动您的设置活动。您可以通过指定其任务关联来完成此操作。这是通过属性完成的android:taskAffinity
。默认情况下,所有活动共享相同的任务关联,默认为清单中指定的主包。在您的设置活动中,您可以指定android:taskAffinity="your.own.package.SettingsTask"
在其自己的任务中启动设置活动。
回答by treed
You're definitely going in the right direction. This is what I have (truncated, because I have all of my activities in the list while I'm devving for fast access):
你肯定是在朝着正确的方向前进。这就是我所拥有的(被截断了,因为我在开发快速访问时将我的所有活动都列在了列表中):
<activity android:name=".DeckDrill"
android:label="DeckDrill">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DeckList"
android:label="DeckList">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I think what may be happening is interference from your action elements which specify the name of your class. I'm pretty sure that actions and categories need to refer to constants. I don't know how that would result in what you're seeing, but you could try removing them. Other than that, you pretty much have what I have.
我认为可能发生的是来自指定类名称的操作元素的干扰。我很确定操作和类别需要引用常量。我不知道这会导致您看到什么,但您可以尝试删除它们。除此之外,你几乎拥有我所拥有的。