Reputation: 1959
I am trying to launch a specific app that appears on the recent apps screen, from a service, given the app's package name. The following usually works:
context.startActivity(new Intent().setPackage(packageName).setAction(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent.FLAG_ACTIVITY_NEW_TASK is required when launching an activity from outside an activity context (in this case an Accessibility service). Even though this would imply that a NEW task will be created, it successfully re-launches the task that was visible in recents, if it already existed. If not, it creates a new one.
However, the above does NOT work when the app in question was originally launched from a different app. For example, if com.android.settings was originally launched from a system notification or from within another app, the above code will launch a new instance of com.android.settings instead of bringing up the one that was already showing in recent apps. I have tried adding different flags to no avail.
Does anyone have a solution for this or at least explain why this happens?
EDIT:
I am adding more steps on how to reproduce this and the output of adb shell dumpsys activity activities.
Steps to reproduce:
context.startActivity(new Intent().setPackage("com.android.settings").setAction(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
OR
context.startActivity(packageManager.getLaunchIntentForPackage("com.android.settings"));
Here is the output of "adb shell dumpsys activity activities" for:
Settings opened through the failed device admin uninstallation.
Settings opened both through the failed device admin uninstallation and also opened by our own code.
Upvotes: 0
Views: 36