Home » Android » android – how to keep multiple activities of the same app in the recent-apps list

android – how to keep multiple activities of the same app in the recent-apps list

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have one app with two activities A & B, both with launch mode singleInstance. I notice that even if both A and B are running in the background, only the last activity is shown in recent-apps list. Is it possible to keep both A & B in the recent-apps list? Thanks.

How to&Answers:

In the AndroidManifest, make sure to set the android:taskAffinity attribute of the element differently for each Activity. For example:

<activity
    android:name="com.example.ActivityA"
    android:label="Activity A"
    android:launchMode="singleInstance"
    android:taskAffinity="com.example.AffinityA" >
</activity>
<activity
    android:name="com.example.ActivityB"
    android:label="Activity B"
    android:launchMode="singleInstance"
    android:taskAffinity="com.example.AffinityB" >
</activity>

Answer:

The trick is to start the new Activity via an Intent with FLAG_ACTIVITY_NEW_TASK flag enabled.