54

I have two activities marked with intent filter

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.package"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="7" />

<application
    android:name=".MyApp"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >

    <!-- work order activity -->
    <activity
        android:name=".app.WorkOrderActivity"
        android:label="@string/work_order"
        android:taskAffinity="com.package.task_for_work_order_activity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <!-- inventory activity -->
    <activity
        android:name=".app.InventoryActivity"
        android:label="@string/inventory"
        android:taskAffinity="com.package.task_for_inventory_activity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

The problem is, that when I'm installing apk file, there is no activity shown, because, as I understood system can't specify what activity should be shown.

I just really need two activities shown in launcher, so please don't give advice about removing intent-filters from second activity, or if you do so provide me another way to show two icons in launcher.

CinCout
  • 9,486
  • 12
  • 49
  • 67
Orest
  • 1,857
  • 5
  • 19
  • 27
  • Tell please more details, why you need it? – Natali Jan 19 '12 at 14:15
  • 1
    because i have two completely separated ui modules, i keep them in different activities and want them both to be accessible from android dashboard. – Orest Jan 19 '12 at 14:18
  • 1
    With the latest Android versions, the above code works fine - with the first activity declared in the android manifest getting preference and shown to the user. – bschandramohan Jun 11 '17 at 18:16

5 Answers5

89

You need to specify which activity is the default one by adding the following line to your intent-filter:

<category android:name="android.intent.category.DEFAULT"/>

Insert this in the default activity and keep the rest. Then it should work.

You may also want to add a different icon to your 2nd activity with the attribute android:icon="@drawable/myothericon"

Adam Rabung
  • 5,232
  • 2
  • 27
  • 42
Mark Gjøl
  • 1,845
  • 12
  • 21
  • 1
    AH this is what I was looking for but I couldn't find it. This is the correct answer. – Mark B Jan 19 '12 at 14:39
  • year, this is userful for me. – wangzhengyi Jan 13 '16 at 08:39
  • 10
    __On latest Android versions__ - If there are more than one launcher activities and if we don't put this `category` tag then the activity which is mentioned first in the Android manifest file will get launched as start-up activity. – RBT Aug 19 '18 at 00:45
  • 1
    When i hav 2 launch activity, that time 2 launch icon display in my mobile. how to remove 2 icon ? – Hitesh Tarbundiya Apr 23 '19 at 11:44
  • 1
    Is the answer outdated or flatout wrong? See https://stackoverflow.com/questions/5727828/what-is-the-purpose-of-android-intent-category-default – Heinzlmaen Aug 02 '19 at 07:38
  • This is not working properly. I have 2 activities.. 1 has action Main and for 2nd activity I added Default category. But It only opens 1st activity (without Default but Main). I want to open 2nd activity without adding Main (as it adds 2 icons). And I can't remove Main from 1st activity as its in a lib. – Tushar Gogna Sep 25 '20 at 08:37
4

Add <action> items with android:name attributes defined to both intent filters:

<application
    android:name=".MyApp"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >

    <!-- work order activity -->
    <activity
        android:name=".app.WorkOrderActivity"
        android:label="@string/work_order"
        android:taskAffinity="com.package.task_for_work_order_activity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name=".app.WorkOrderActivity"/>
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <!-- inventory activity -->
    <activity
        android:name=".app.InventoryActivity"
        android:label="@string/inventory"
        android:taskAffinity="com.package.task_for_inventory_activity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name=".app.InventoryActivity"/>
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
a.ch.
  • 8,285
  • 5
  • 40
  • 53
3

I think it's because of the "android.intent.action.MAIN" action, this worked for me :

<application>
    <activity android:name=".MyActivityA" android:icon="@drawable/aicon"  android:label="@string/app_name">
        <intent-filter>
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MyActivityB" android:icon="@drawable/bicon" android:label="@string/app_name">
        <intent-filter>
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
 </application>
Reno
  • 33,594
  • 11
  • 89
  • 102
  • 2
    I tried that, but in that case second activity doesn't appear in my launcher.. i can't find the solution of it..maybe it's problem of android 2.1 – Orest Jan 19 '12 at 14:21
  • No i tried removing second MAIN action and running it on emulator 2.3.3 and no use – Orest Jan 19 '12 at 14:24
3

Seems it is possible to have multiple startup activities by using the task affinity property. See the answer at https://stackoverflow.com/a/3270422/819904

Community
  • 1
  • 1
Ruddles
  • 193
  • 2
  • 11
  • 2
    Never post answers that are basically "elsewhere"... the link may get removed, or changed.... even then, its like you are showing other users like this: "You want to solve this problem? Here, go there...".... then becomes "Circular logic works because Circular logic works because Circular logic works because `goTo(charAt(0)`" – Bonatti Mar 23 '16 at 19:31
  • Please, read this link on: "[Provide context for links](http://stackoverflow.com/help/how-to-answer)" that states: Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. – Bonatti Mar 24 '16 at 11:08
  • I am just asking you to answer a question, by giving the answer, not redirecting anyone anywhere else (even "inside" stackoverflow domain), likewise, the owner of that question can "delete" the question, the answer can be eddited, and even the question itself be closed..... What I am asking of you is to improve the way you answer stuff, in a way that helps others. – Bonatti Mar 24 '16 at 11:21
2

I tested on an emulator running API level 29. The following is enough:

<activity
    android:name=".MyActivity"
    android:label="Activity 1"
    android:exported="true">

    <intent-filter>
        <category android:name="android.intent.category.LAUNCHER" />
        <action android:name="android.intent.action.MAIN" />
    </intent-filter>

</activity>

<activity
    android:name=".MyActivity2"
    android:label="Activity 2"
    android:exported="true">

    <intent-filter>
        <category android:name="android.intent.category.LAUNCHER" />
        <action android:name="android.intent.action.MAIN" />
    </intent-filter>

</activity>

The reason some say this doesn't work is because if the app is already running, it will resume running the last activity in the app no matter which of your 2 activities on the launcher you click on. But try (force) stopping the app, and then you will see the activity corresponding to the one you click on in the launcher starts.

This behavior is usually not what we want, so we can start running the activities in different tasks by using <activity android:taskAffinity="an.identifier.for.the.task">, e.g.:

<activity
    android:name=".MyActivity"
    android:label="Activity 1"
    android:exported="true"
    android:taskAffinity=".task.sellers">

    <intent-filter>
        <category android:name="android.intent.category.LAUNCHER" />
        <action android:name="android.intent.action.MAIN" />
    </intent-filter>

</activity>

<activity
    android:name=".MyActivity2"
    android:label="Activity 2"
    android:exported="true"
    android:taskAffinity=".task.buyers">

    <intent-filter>
        <category android:name="android.intent.category.LAUNCHER" />
        <action android:name="android.intent.action.MAIN" />
    </intent-filter>

</activity>
Peppe L-G
  • 7,351
  • 2
  • 25
  • 50
  • I have this solution on my smartphone version of the app. And it is working correctly. But this is not working on my watch version of this app on android 11. Any idea what is the cause? – Kacper Sierakowski Mar 08 '23 at 13:12
  • @KacperSierakowski, Sorry, but I have no idea (I've never used Wear OS). – Peppe L-G Mar 08 '23 at 15:35