Home > Mobile >  Can PendingIntent.FLAG_IMMUTABLE replace PendingIntent.FLAG_UPDATE_CURRENT?
Can PendingIntent.FLAG_IMMUTABLE replace PendingIntent.FLAG_UPDATE_CURRENT?

Time:08-11

I'm dealing with PendingIntent with notification. In my project, I've been using PendingIntent.FLAG_UPDATE_CURRENT in some of code. and the definition of it is below.

Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent. For use with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), and getService(Context, int, Intent, int).

This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.

FLAG_UPDATE_CURRENT still works even if FLAG_IMMUTABLE is set - the creator of the PendingIntent can always update the PendingIntent itself. The IMMUTABLE flag only limits the ability to alter the semantics of the intent that is sent by send() by the invoker of send().


But I have to choose between FLAG_IMMUTABLE or FLAG_MUTABLE because of Android12. In my case, i don't need to use PendingIntent.FLAG_MUTABLE. the definition of it is below.

Flag indicating that the created PendingIntent should be immutable. This means that the additional intent argument passed to the send methods to fill in unpopulated properties of this intent will be ignored.

FLAG_IMMUTABLE only limits the ability to alter the semantics of the intent that is sent by send() by the invoker of send(). The creator of the PendingIntent can always update the PendingIntent itself via FLAG_UPDATE_CURRENT.

as you can see, they say "FLAG_UPDATE_CURRENT still works even if FLAG_IMMUTABLE is set..." and "The creator of the PendingIntent can always update the PendingIntent itself via FLAG_UPDATE_CURRENT."

So, what i want to know is that FLAG_IMMUTABLE can perfectly replace FLAG_UPDATE_CURRENT in any Android version?

For example

PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
=> PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_IMMUTABLE);

CodePudding user response:

FLAG_IMMUTABLE and FLAT_MUTABLE controls if other apps can modify your PendingIntent. For instance, if you were using a direct reply action in a notification, the system would need you to use FLAT_MUTABLE to allow it to fill in the text the user typed and send it to you.

FLAG_UPDATE_CURRENT is the ability for your app to update its own PendingIntent. This means if you create the same PendingIntent where the only difference is the extras attached to your Intent, FLAG_UPDATE_CURRENT would ensure that your new extras are actually used (instead of just the original set being used again).

So in every case where you were using FLAG_UPDATE_CURRENT, you'd want to continue to use FLAG_UPDATE_CURRENT, adding in the correct mutability flag for your case (which, in 99% of cases, is FLAG_IMMUTABLE).

To apply both flags, use the | symbol in Java (or the word or in Kotlin):

PendingIntent.getActivity(context, requestCode, intent,
    PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
  • Related