What's "requestCode" used for on PendingIntent?

AndroidAndroid IntentAlarmmanagerAndroid Pendingintent

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

  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.

Solution 2 - Android

I just want to add to @Minhaj Arfin answer

1- requestCode is used to get the same pending intent later on (for cancelling etc)

2- Yes, they will get override as long as your specify the same Receiver to your Intent that you specify on your PendingIntent

example:

Intent startIntent1 = new Intent(context, AlarmReceiverFirst.class);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 0, startIntent1, 0);

Intent startIntent2 = new Intent(context, AlarmReceiverSecond.class);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, 0, startIntent2, 0);

From above example, they will not override each other because the receiver is different(AlarmReceiverFirst and AlarmReceiverSecond)

Intent startIntent2 = new Intent(context, AlarmReceiverSecond.class);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, 0, startIntent2, 0);

Intent startIntent3 = new Intent(context, AlarmReceiverSecond.class);
PendingIntent pendingIntent3 = PendingIntent.getBroadcast(context, 0, startIntent3, 0);

From above example, they will override each other, because the receiver is same(AlarmReceiverSecond)

Solution 3 - Android

Actually, the documentation clearly states what the request code is used for:

> If you truly need multiple distinct PendingIntent objects active at > the same time (such as to use as two notifications that are both shown > at the same time), then you will need to ensure there is something > that is different about them to associate them with different > PendingIntents. This may be any of the Intent attributes considered by > Intent#filterEquals(Intent), or different request code integers > supplied to getActivity(Context, int, Intent, int), > getActivities(Context, int, Intent[], int), getBroadcast(Context, int, > Intent, int), or getService(Context, int, Intent, int).

Since it seems that it still isn't that clear, let me try to explain:

When you want to use a PendingIntent object, you don't just instantiate one. Rather, you obtain one from the system using the PendingIntent static methods (getActivity, getBroadcast, getService etc). The system keeps a bunch of PendingIntent instances and gives you one. Which one it gives you, it depends on the input parameters you pass to these getter methods. Those input parameters are: Context, i.e. the target receiver of the intent, the Intent to use, requestCode and flags. When you pass the same Context, the same requestCode and the same Intent (meaning an intent that filterEquals with another intent), you get the same PendingIntent object. The point is that the system wants to have as few PendingIntent objects as possible, so it tends to reuse the existing ones, as much as possible.

For example, you have two calendar notifications, for two different dates. When you click on one of them, you want your app to open to the corresponding date of that notification. In that scenario, you have the same Context target, and the Intent object you are passing differ only in the EXTRA_DATA (which specifies the date that should be open). If you provide the same requestCode when obtaining the PendingIntent object, then you will end up with the same PendingIntent object. So, when creating the second notification, you will replace the old Intent object with the new EXTRA_DATA, and end up with two notifications pointing to the same date.

If you want to have two different PendingIntent objects, as you should in this scenario, you should specify a different requestCode when obtaining the PendingIntent object.

Solution 4 - Android

in my case i want to open the same activity with two different intents so if two or more FCMS are there in the tray, any one of them will only open other will not, so i changed the requests codes of pending intent then it worked.

 PendingIntent pendingIntent =
                            PendingIntent.getActivity(this, **Some unique id for all GCMS** /* Request code */, intent,
                                    PendingIntent.FLAG_ONE_SHOT);

Solution 5 - Android

one important thing about requestCode that will seriously trouble your app is when using widgets. widgets will not work after phone reboot if their requestCode are the same. that means the pendingIndent you set on the remoteViews of your widget must be set unique requestCode, usually the widgetId accompanying a number.

Solution 6 - Android

when building a PendingIntent, you are required to provide an integer called requestCode. The official documentation states that this is a “private request code for the sender”. However, request codes are also a fundamental way to distinguish between PendingIntent instances. You see, the getBroadcast() method is much more than a just a static factory for creating a PendingIntent instance

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionandroid developerView Question on Stackoverflow
Solution 1 - AndroidMinhaj ArfinView Answer on Stackoverflow
Solution 2 - AndroidHendraWDView Answer on Stackoverflow
Solution 3 - AndroidEirView Answer on Stackoverflow
Solution 4 - AndroidJSONParserView Answer on Stackoverflow
Solution 5 - AndroidAlireza JamaliView Answer on Stackoverflow
Solution 6 - AndroidLakshay SharmaView Answer on Stackoverflow