如何获得多个图标以在一个应用程序中启动不同的活动? [英] How do I get multiple icons to launch different activities in one application?
问题描述
我有一个包含两个活动的应用程序,我希望能够在启动器中显示两个图标,每个图标启动应用程序中的相应活动.
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?
推荐答案
您需要做的是在另一个任务中启动您的设置活动.您可以通过指定其任务关联来完成此操作.这是通过属性 android:taskAffinity
完成的.默认情况下,所有活动共享相同的任务关联,默认为清单中指定的主包.在您的设置活动中,您可以指定 android:taskAffinity="your.own.package.SettingsTask"
以在其自己的任务中启动设置活动.
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.
这篇关于如何获得多个图标以在一个应用程序中启动不同的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!