[英]Two main activities in AndroidManifest.xml
I would like to have two main activities in my app.我想在我的应用程序中有两个主要活动。 So in my manifest I put:
所以在我的清单中我放了:
<activity
android:name="mypackage1.MainActivity"
android:label="@string/title_activity_main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="mypackage2.MainActivity2"
android:label="@string/title_activity_main2">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Two icons are created in my apps menu.在我的应用程序菜单中创建了两个图标。 But when I click on each of them the first activity MainActivity is always launched.
但是当我点击它们中的每一个时,第一个活动 MainActivity 总是被启动。 Is it possible to have two main activities?
是否有可能有两个主要活动? If so, what's wrong with what I did?
如果是这样,我所做的有什么问题? Thanks
谢谢
The LAUNCHER
intent filter is what determines what shows up in the app drawer/launcher. LAUNCHER
意图过滤器决定了应用程序抽屉/启动器中显示的内容。 That is why you get two icons shown up.这就是为什么会显示两个图标的原因。
However, you also set the DEFAULT
intent filter, which sets the default Activity
for the whole package.但是,您还设置了
DEFAULT
意图过滤器,它为整个包设置了默认Activity
。 Since you set it twice, you get the problem of precedence of the first/latest registered.由于您设置了两次,您会遇到第一个/最新注册的优先级问题。 When you remove the
DEFAULT
filter, you will be able to start whatever you click on in the launcher.当您删除
DEFAULT
过滤器时,您将能够启动您在启动器中单击的任何内容。
In short, remove the following line from both Activities:简而言之,从两个活动中删除以下行:
<category android:name="android.intent.category.DEFAULT" />
Yes, just mark two or more of your <activity>
s as LAUNCHER
within your manifest.是的,只需在清单中将两个或多个
<activity>
标记为LAUNCHER
。 In addition you have to set the android:taskAffinity
attribute on both of your Launcher-Activities which specify the exact package and Activity to be started.此外,您必须在两个 Launcher-Activities 上设置
android:taskAffinity
属性,以指定要启动的确切包和活动。
<activity android:label="MyApp" android:name=".MyApp" android:taskAffinity="com.example.MainActivity">
<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" android:taskAffinity="com.example.SettingsActivity" >
<intent-filter>
<action android:name=".Settings"/>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Use android:documentLaunchMode="intoExisting"
, to launch a separate task based on the intent's Component name and data URI.使用
android:documentLaunchMode="intoExisting"
,根据意图的组件名称和数据 URI 启动单独的任务。 Without this (by default), the activity will share all the same activities, as android:documentLaunchMode
default to none
.没有这个(默认情况下),活动将共享所有相同的活动,因为
android:documentLaunchMode
默认为none
。
intoExisting: The system searches for a task whose base intent's ComponentName and data URI match those of the launching intent.
intoExisting:系统搜索基本意图的ComponentName 和数据 URI与启动意图匹配的任务。 If the system finds such a task, the system clears the task, and restarts with the root activity receiving a call to onNewIntent(android.content.Intent).
如果系统发现这样的任务,系统会清除该任务,并以接收 onNewIntent(android.content.Intent) 调用的根活动重新启动。 If the system does not find such a task, the system creates a new task.
如果系统没有找到这样的任务,系统会创建一个新任务。 source
来源
<activity
android:name=".CameraActivity"
android:exported="true"
android:documentLaunchMode="intoExisting"
android:label="@string/app_1_label">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ProfilePoseNetActivity"
android:exported="true"
android:documentLaunchMode="intoExisting"
android:label="@string/app_2_label">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
The intention of potroae using task affinities to prevent your 2 activities from sharing the same task. Potroae使用任务亲缘关系来防止您的 2 个活动共享同一任务的意图。 However, it is annoying to have to select a task name, ie
com.example/SettingsActivity
for every task you want to launch separately.但是,必须为要单独启动的每个任务选择一个任务名称,即
com.example/SettingsActivity
很烦人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.