Binabik
Binabik

Reputation: 1073

FLAG_ACTIVITY_NEW_TASK starts multiple tasks with the same activity

I have a Notification which starts an Activity B with FLAG_ACTIVITY_NEW_TASK. The documentation says:

[...] if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front [...]

In the case that the user taps the Notification while in Home Screen or a different app, this works fine. But if the user is in an Activity of my app, let's say in Activity "A", and then taps the Notification multiple times, Activity "B" is started multiple times. This leads to Back key not bringing back to Activity "A".

What am I doing wrong here? And where's the difference between being in home screen or a different app and being in Activity A of my app?

Regards

Upvotes: 1

Views: 2817

Answers (2)

Binabik
Binabik

Reputation: 1073

Now I found out that I had to set a different affinity in the manifest to start the Activity in its own task. That solved my problem, too.

Upvotes: 2

Nikolay Elenkov
Nikolay Elenkov

Reputation: 52936

You probably want FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP for the notification activity. That will make sure that there is only one instance of Activity B, and it is re-used .

The difference is that when your Activity A is in the foreground you already have task running , otherwise (most probably) not. Read this for more info about tasks.

Upvotes: 0

Related Questions