What's "requestCode" used for on PendingIntent?

android developer picture android developer · Feb 3, 2014 · Viewed 36.7k times · Source

Background:

I'm using PendingIntent for alarms via AlarmManager.

The problem:

At first I thought that in order to cancel previous ones, I must provide the exact requestCode that I've used before to start the alarm.

But then I've found out I was wrong, as the cancellation API says:

Remove any alarms with a matching Intent. Any alarm, of any type, whose Intent matches this one (as defined by filterEquals(Intent)), will be canceled.

looking at "filterEquals", the documentation says:

Determine if two intents are the same for the purposes of intent resolution (filtering). That is, if their action, data, type, class, and categories are the same. This does not compare any extra data included in the intents.

so I don't get what the "requestCode" is for...

The question:

What is "requestCode" used for?

What if I create multiple alarms with the same "requestCode" ? do they override each other?

Answer

Minhaj Arfin picture Minhaj Arfin · Feb 3, 2014
  1. requestCode is used to retrieve the same pending intent instance later on (for cancelling, etc).
  2. Yes, my guess is the alarms will override each other. I would keep the request codes unique.