What is the meaning of use FLAG_ONE_SHOT in a pendingintent which will be used to a notification? Is it necessary?

Viewed 187
Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setContentIntent(pendingIntent);

Does it mean that I can't jump to the mainActivity multiple times by repeatedly clicking this notification? Is pendingIntent.send() called internally when I clicks the notification? Does this flag become meaningless in the notifications?

1 Answers

PendingIntent.FLAG_ONE_SHOT means that the PendingIntent will be deleted after it is used once. Normally, a PendingIntent can hang around if there are other references to it in the system.

This flag can be used to prevent multiple uses of the PendingIntent, although my personal experience is that this flag generally causes more problems than it solves.

You asked:

Is pendingIntent.send() called internally when I clicks the notification?

Yes, that's basically what happens when you click on the UI element associated with the PendingIntent in the Notification.

Related