My application has two activities A and B. A is the root of the task, and is the one that is launched from the launch icon. B can be started from A.
As well as starting A from the launch icon, it is possible to launch A by clicking on a file in another application, e.g. clicking on an email attachment or a file in Drive. I have done this by adding actions and categories to the intent filter in the manifest file.
I want to make it so that when A is launched from another application, instead of creating a new task I want the existing task to resume in the same state it was in before. This could be activity A or B, wherever the user happened to be before they pressed home.
I have tried all kinds of launch modes and intent flags but nothing seems to work.
Broadcast an intent that's identical to the launcher intent in your manifest:
Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
I don't know why, but this raises the existing task instead of starting a new one. By contrast, a launcher intent obtained the "official" way will actually start a new task:
Intent intent = context.getPackageManager()
.getLaunchIntentForPackage(context.getPackageName());