当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Two main activities in AndroidManifest.xml

问题描述:

I would like to have to 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. 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. 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. 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.

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

<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>
分享给朋友:
您可能感兴趣的文章:
随机阅读: