问题
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.
回答1:
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:
- https://stackoverflow.com/a/14991392/769265
- https://stackoverflow.com/a/36127362/769265
- https://stackoverflow.com/a/18622131/769265
来源:https://stackoverflow.com/questions/64769530/not-working-corss-application-activities-with-taskaffinity-in-android-11