Opening multiple android tasks with the same root activity

1k Views Asked by At

I'm trying to start an intent with Main2Activity in a new (separate) task (i.e. separate back stack and separate instance in recents screen). I realized that the way it should be done is adding the Intent.FLAG_ACTIVITY_NEW_TASK and Intent.FLAG_ACTIVITY_MULTIPLE_TASK intent flags:

Intent intent = new Intent(this, Main2Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
startActivity(intent);

But this only works if I set (in the manifest) Main2Activity's android:taskAffinity to other than the default (i.e. other than the current activity's task affinity).

The problem is that I want to be able to create another new task with the same activity more than once.

Example:

In the android Gmail app, you can click the "compose" button several times, and each time it creates a "new instance" of the composer. I'd like to achieve the exact same behavior.

Any clue?

Thanks a lot.

2

There are 2 best solutions below

0
On

Ok, so I think I just figured it out.

Replaced the flag Intent.FLAG_ACTIVITY_NEW_TASK with Intent.FLAG_ACTIVITY_NEW_DOCUMENT and it seems to work fine.

note: this flag requires API level >= 21

0
On

Instead of using

Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK

You have to use only

Intent.FLAG_ACTIVITY_MULTIPLE_TASK

because documents says that FLAG_ACTIVITY_NEW_TASK brings the old task in front if already started and create new task only if not running.