11

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.

hixhix
  • 771
  • 7
  • 23

2 Answers2

15

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>
JayWozz
  • 183
  • 1
  • 8
  • 2
    This should be the accepted answer, FLAG_ACTIVITY_NEW_TASK actually check for task already present where as defining this flag above definition in manifest does create a separate task and you can see different activity present in recent apps stack. – silentsudo Jan 15 '18 at 10:40
  • This must be the accepted answer as it helped me in 2020 and it nearly took my whole day to reach to solution via this answer. It throws brightest light over the question. Thanks @JayWozz and referred you here https://stackoverflow.com/q/60349723/9810130 in my question – Rushikant Pawar Feb 22 '20 at 09:31
  • Up vote and Thanks for making this answer available here over SO – Rushikant Pawar Feb 22 '20 at 09:32
0

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

Sagar
  • 3,107
  • 2
  • 26
  • 35