In the document of the Notification
class I see this:
public PendingIntent contentIntent
The intent to execute when the expanded status entry is clicked. If this is an activity, it must include the
FLAG_ACTIVITY_NEW_TASK
flag, which requires that you take care of task management as described in the Tasks and Back Stack document. In particular, make sure to read the notification section Handling Notifications for the correct ways to launch an application from a notification.
I've read the materials linked from the above, but I still don't get it. Why is the FLAG_ACTIVITY_NEW_TASK
flag required when an activity is to be started from clicking a notification? I tried the following code:
NotificationManager manager = (NotificationManager)context.
getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(
android.R.drawable.stat_notify_sync, title,
System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(context, NotifiedActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // IS THIS REALLY REQUIRED??
PendingIntent pt = PendingIntent.getActivity(context, 0, intent, 0);
notification.setLatestEventInfo(context, title, text, pt);
manager.notify(0, notification);
I ran the above code, both with and without the intent.setFlags
line, and there seems to be no difference. In fact, many code samples I found simply don't have that line. So why does the document says that the FLAG_ACTIVITY_NEW_TASK
flag is a must, and exactly what difference does it make in notification handling?