Android Notification: Intent.flag_activity_new_task Required?
Solution 1:
Technically this flag is required. But since it is required, Android is nice and will just set it for you ;-)
The reason it is required is as follows:
The code that processes the Notification
and calls startActivity()
to actually launch the Intent
, isn't running in a "task". It is system code, part of the Notification
system. Normally, if you call startActivity()
and the flag Intent.FLAG_ACTIVITY_NEW_TASK
is not set, Android will try to launch that Activity into the current task (ie: the task containing the Activity that is calling startActivity()
). Since, in this case, there is no task, Android must launch the Activity into another task. That's why you need to specify Intent.FLAG_ACTIVITY_NEW_TASK
.
In practice, this won't always create a new task, since Android will first try to find an (suitable) existing task to launch the Activity into. If your app is already running, Android will just launch the Activity into that task. (This isn't 100% true, there are other special cases that can change this logic, but I'm not going to address them here).
NOTE: The same situation exists if you call startActivity()
from a Service
or a BroadcastReceiver
. In those cases, the flag Intent.FLAG_ACTIVITY_NEW_TASK
must be set, because there is no "current task", so Android must launch the Activity into another task.
Post a Comment for "Android Notification: Intent.flag_activity_new_task Required?"