Android app with two activities as enter points

85 Views Asked by At

App contains of two activities as enter points as desctibed in manifest.

    <activity
        android:name=".ui.screen.main.MainActivity"
        android:icon="@mipmap/ic_tracker_launcher" >
        <intent-filter android:label="@string/app_name" >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".ui.screen.main.MapEnterActivity"
        android:icon="@mipmap/ic_watcher_launcher" >
        <intent-filter android:label="@string/app_name_watcher" >
            <action android:name="android.intent.action.MAIN" />

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

So as a result we have two icons at mobile desktop.

The problem I need to solve is when one of icons launched corresponding activity and user press "back" button (app not killed) to press another icon and launch another activity - he couldn't run another activity via its icon and the first one appears again. So it should be killed before running another activity correctly.

Is it possible to run each activity via its icon without killing app manually (need to be comfortable for user) and how to implement this?

Many thanks for any suggestions.

1

There are 1 best solutions below

1
On BEST ANSWER

add android:taskAffinity on each activity

 <activity
    android:taskAffinity="com.example.MainActivity"
    android:name=".ui.screen.main.MainActivity"
    android:icon="@mipmap/ic_tracker_launcher" >
    <intent-filter android:label="@string/app_name" >
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:taskAffinity="com.example.MapEnterActivity"
    android:name=".ui.screen.main.MapEnterActivity"
    android:icon="@mipmap/ic_watcher_launcher" >
    <intent-filter android:label="@string/app_name_watcher" >
        <action android:name="android.intent.action.MAIN" />

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