Issue with taskAffinity and excludeFromRecents on Android 10

1.6k views Asked by At

I have an application which is sometimes displaying notifications to users. When pressing a notification, a "Dispatch" Activity is triggered which calls startActivities with some intents leading to proper activity depending on the notification payload. Here is how it is declared in Manifest according to https://developer.android.com/training/notify-user/navigation#ExtendedNotification

            android:name=".DispatchActivity"
            android:taskAffinity=""
            android:excludeFromRecents="true"
            android:launchMode="singleTask" />

When I press a notification, DispatchActivity is launched with Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK and launches a new activity with Intent.FLAG_ACTIVITY_NEW_TASK before it immediately calls finish(). If my application was already launched, the new activity (let's call it the second Activity) is launched on top of previous already launched activity so when you press Recents button, you can see my app only once.

enter image description here

enter image description here

This is actually the behaviour I want. The thing is, when I run the same code on Android 10 and press Recents button, I can see two instances of my app (well two tasks I guess) :

enter image description here

If I press Recent button again to restore the second Activity, it just goes away and I only have the initial Activity. As if SecondActivity inherited from excludeFromRecents behaviour where this is not the case on other Android versions (I even tested on Android 11 preview and it's working as expected).

I tried removing taskAffinity="" for DispatchActivity in Manifest so it has same affinity as other Activities in the application, but I then have issues when receiving a notification with my app not launched where activities launched from DispatchActivity are going away like initially on Android 10 because of excludeFromRecents tag.

I'm running out of idea right now so if you have any solution or maybe have already met the problem I'm open for suggestions.

Thanks !

Here is the sample code if needed : https://github.com/LoicJ/MultiTaskBugSample.git

1

There are 1 answers

4
Deejayen On

The answer is android:noHistory="true" add this line to your activity in the manifest file it will provide your expected behavior.

Your final code may look like:

<activity
        android:name=".DispatchActivity"
        android:excludeFromRecents="true"
        android:launchMode="singleTask"
        android:noHistory="true"
        android:taskAffinity="" />