1

I know the concept of pending intent but the flags are confusing.

Even android documentation is very hard to understand

Can someone provide explanation of pending intent flags particularly FLAG_ONE_SHOT and FLAG_NO_CREATE with examples?

1 Answers1

1

PendingIntents are managed by the Android framework. When you call one of the PendingIntent.getXXX() methods, the framework tries to find an existing PendingIntent that matches the parameters you pass to the getXXX() method. If it finds a matching PendingIntent it will just return that to the caller. If it doesn't find a matching PendingIntent it will (usually) create a new PendingIntent and return that to the caller. You can alter this standard behaviour by using the flags:

  • FLAG_NO_CREATE is used to get an existing PendingIntent. If a matching PendingIntent exists, it will be returned to the caller. If no matching PendingIntent exists, nothing happens. The framework will not create a new PendingIntent and the method returns null to the caller. You can use this method to determine if a specific PendingIntent exists. You can also use this method to get an existing PendingIntent so that you can cancel it.

  • FLAG_ONE_SHOT is strange. According to the documentation, this flag should cause the PendingIntent to be deleted after it is used (sent). However, there are other side-effects of this flag. For example, if you create a PendingIntent using this flag, and then try to get this PendingIntent (or test the existence of it) by calling PendingIntent.getXXX() with FLAG_NO_CREATE, the framework will always return null. For this reason I never use it and I also recommend that it never be used.

  • FLAG_CANCEL_CURRENT is used to delete an existing PendingIntent and create a new one. The framework first tries to find a matching PendingIntent. If it finds one, it cancels (deletes) this PendingIntent. That means that any applications holding this PendingIntent will not be able to trigger (send) it. The framework then creates a new PendingIntent with the provided parameters and returns this to the caller.

  • FLAG_UPDATE_CURRENT is used to update an existing PendingIntent. The framework first tries to find a matching PendingIntent. If it finds one, the "extras" in the existing PendingIntent are overwritten with the "extras" in the provided Intent parameter. If no matching PendingIntent is found, a new one is created with the provided parameters. The found (or newly created) PendingIntent is returned to the caller.


NOTE: See this answer for information on how the Android framework tries to find a "matching" PendingIntent: https://stackoverflow.com/a/29590084/769265

David Wasser
  • 85,616
  • 15
  • 182
  • 239
  • Will pending intent be running in the background? – ADITYA DIXIT May 06 '21 at 09:56
  • I don't understand what you mean. `PendingIntent`s don't "run". They are sent. Depending on the `Intent` that is wrapped by the `PendingIntent`, this can result in starting an `Activity`, or a `BroadcastReceiver`, or a `Service`. Please explain what you are asking. – David Wasser May 06 '21 at 14:18
  • In this sentence `When you call one of the PendingIntent.getXXX() methods, the framework tries to find an existing PendingIntent that matches the parameters you pass to the getXXX() method`, you say that `it tries to find an existing intent`. My doubt was whether a pending intent will be running in the background? What exactly do you mean by that sentence? – ADITYA DIXIT May 07 '21 at 05:11
  • In this [question](https://stackoverflow.com/questions/29778294/pending-intent-with-one-shot-flag/29780579#29780579) I have posted a comment, can you answer that? – ADITYA DIXIT May 07 '21 at 05:12
  • I saw your comment but do not understand it either. You quote a passage that isn't in the question. Sorry. – David Wasser May 07 '21 at 06:53
  • I added a link to an answer that I wrote about how Android tried to find a matching `PendingIntent`. I still don't understand what you mean about "running in the background" – David Wasser May 07 '21 at 06:58
  • In the question asked by @greywolf82, after the code there is the sentence `If in the meantime someone asks for a new alarm, if the alarm exist I don't want to setup anything` – ADITYA DIXIT May 07 '21 at 13:58
  • Yes, you are right. I see that now. I assume that he means that if he has already set an alarm, and it has not triggered yet, he doesn't want to set another one. He wants to just leave the original alarm. This is why he uses "FLAG_NO_CREATE". He is using this to test if the `PendingIntent` already exists. Unfortunately that doesn't work if he is using FLAG_ONE_SHOT which I note in my answer above. – David Wasser May 07 '21 at 14:17