Flyview
Flyview

Reputation: 1959

Re-launch an existing task in recent apps from a service

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:

  1. Clear all tasks
  2. Try to uninstall a device admin app directly from the launcher by dragging it to the "uninstall" button. It will fail and show a notification.
  3. Tap the notification to open up the Settings app to the device admin page.
  4. Launch com.android.settings from code:

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"));
  1. Open up the recent apps screen to verify whether there are now 2 Settings tasks present.

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

Answers (0)

Related Questions