3

I have two application, App A and App B, now I want to start an activity in App B from App A, the launchmode of this Activity is “singleTask”.

The order: Activity X (App A) ———> Activity Y (App B) ———> Activity Z (App B, launchMode=“singleTask”)

As default AndroidManifest config, there will be two App shown in the Task Manager, I hope users can only see App A label in Task Manager, so when they switch between tasks they don’t click App B. Before Android 11(Android R), I use the attribute taskAffinity as below to solve this problem.

Both Activity X and Activity Z, add this in AndroidManifet.xml

android:taskAffinity="com.abc.xxx”

So these activity can both house in one activity task. In Android 10, it works.

However, it doen’t work in Android11 anymore. And, I didn’t find any new features relevant to this scene.

How can I make ApplicationA’s activity task house the activity of ApplicationB which launchmode is singleTask? Let users see only one task(ApplicationA) in task manager.

YikFung
  • 31
  • 1

1 Answers1

5

You can't. And you shouldn't. The reason it was working before is that taskAffinity was trumping (overriding) the launchMode. Obviously they have changed/fixed that in Android 11.

If an Activity is declared as singleTask then this tells Android that the Activity wants to be the root Activity in its own task. When you launch this Activity, it should be launched in a new task, not into the same task as the Activity doing the launching.

In earlier versions of Android, the Activity would be launched into the same task if the Activity had the sametaskAffinity as the root Activity of the launching task. This was never clearly documented, and so it wasn't clear if this was a "bug" or a "feature". It looks like they have finally changed/fixed this in Android 11.

See my answers to these related questions:

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • So how can I let users see only one task when they switch tasks? – YikFung Nov 11 '20 at 04:02
  • Remove the special launch mode `singleTask` from the `` declaration. Generally this launch mode is not required. – David Wasser Nov 11 '20 at 09:59
  • This maybe right. But through https://developer.android.com/guide/components/activities/tasks-and-back-stack#Affinities this document point out and explain that it can be used across applications. And I test Android 10 is working but Android 11 is not. So this may be a bug and fixed in Android 11 – likeme Sep 28 '22 at 10:07