Alex Rivera | Logout

Is it necessary to use FLAG_ACTIVITY_NEW_TASK in a Notification's PendingIntent?

Asked 2012-08-17T06:50:33.887
8

I have been using Notifications for a while, and yesterday I noticed that the documentation of PendingIntent says that the Intent that is passed to the PendingIntent.getActivity()method must have the FLAG_ACTIVITY_NEW_TASK set:

Note that the activity will be started outside of the context of an existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK launch flag in the Intent.

However, I have never set this flag when using Notifications, and yet so far I have not experienced any problem. I have seen out there several examples of Notifications where the FLAG_ACTIVITY_NEW_TASK is not set for the Intent that the PendingIntent is referencing. In particular, the official guide shows the snippet below:

Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

And as you can see, they are not setting the FLAG_ACTIVITY_NEW_TASK flag. So my question is, should I always set the FLAG_ACTIVITY_NEW_TASK flag when using PendingIntent.getActivity(), or are there some scenarios in which it can be omitted? In particular, when using Notifications, can I use an Intent without setting this flag?

Edit
Report

1 Answer

1

I believe the system sets it for you for notifications. You will get a new task when launched from a notification.

answered 2012-12-13T22:46:23.830

Your Answer