11

Similar to this question, but not the same

After update to Android 12 (SDK 31) we change PendingIntent.getActivity(context, 0, intent, 0) to PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE) like suggested.

But PendingIntent.FLAG_IMMUTABLE is not available for SDK under 23. If I add if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) to keep both versions, I stay with the lint warning about not using the correct flag for the else case.

What is the expected behaviour here? Thanks!

Canato
  • 3,598
  • 5
  • 33
  • 57
  • 3
    If you have the version check then you can choose to either ignore the lint warning or suppress it – tyczj Sep 07 '21 at 12:57
  • @tyczj indeed, but I hope there is the right way fo doing it, that is not ignore/suppress. – Canato Sep 07 '21 at 13:13
  • that is the right way of doing it, IDE is not smart enough to know you handled the scenario – tyczj Sep 07 '21 at 13:16

3 Answers3

6

I just got the same problem, here is how i fixed it:

val flags =
  if (SDK_INT >= Build.VERSION_CODES.S) {
    PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
  } else {
    PendingIntent.FLAG_UPDATE_CURRENT
  }

  return Intent(context, TastingReceiver::class.java).let { intent ->
    intent.putExtra(EXTRA_TASTING_ID, tasting.id)
    PendingIntent.getBroadcast(
    context,
    tasting.id.hashCode(),
    intent,
    flags
   )
}
Ninjinski
  • 227
  • 3
  • 10
  • 3
    A good answer will always include an explanation why this would solve the issue, so that the OP and any future readers can learn from it. – Tyler2P Dec 15 '21 at 16:24
  • 2
    I dont really know why tbh, this just a way to put it that the IDE doesn't complain with – Ninjinski Dec 16 '21 at 11:38
1

From the documentation of PendingIntent.FLAG_MUTABLE:

Up until Build.VERSION_CODES.R, PendingIntents are assumed to be mutable by default, unless FLAG_IMMUTABLE is set. Starting with Build.VERSION_CODES.S, it will be required to explicitly specify the mutability of PendingIntents on creation with either FLAG_IMMUTABLE or FLAG_MUTABLE. It is strongly recommended to use FLAG_IMMUTABLE when creating a PendingIntent. FLAG_MUTABLE should only be used when some functionality relies on modifying the underlying intent, e.g. any PendingIntent that needs to be used with inline reply or bubbles.

In summary, you should add the FLAG_IMMUTABLE flag to your PendingIntent when targeting API 31 or later, unless you need your PendingIntent to be mutable, in which case you need to use FLAG_MUTABLE.

Because FLAG_IMMUTABLE was introduced in API 23, you have to use FLAG_MUTABLE as a fallback for lower versions.

val flag =
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE
  else PendingIntent.FLAG_MUTABLE

You can combine this with your existing intent flags, if you have any, using a bitwise or operation. For example:

val flags = flag or PendingIntent.FLAG_ONE_SHOT
Noah
  • 2,718
  • 3
  • 17
  • 23
  • 2
    val flag = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE else PendingIntent.FLAG_MUTABLE this code will not solve the OP's problem as PendingIntent.FLAG_MUTABLE was first introduced in api level 31 so it cannot be used for lower versions (<23 /Build.VERSION_CODES.M in this case) as @Noah mentioned in his answer. Lint will keep giving the warning. – Muhammad Ahmed Jan 21 '22 at 13:53
  • 1
    My apps widget is not working in Samsung galaxy models version 11 and 12. I don't have any idea, why it is freezing now PendingIntent launchAppActionPending = null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { launchAppActionPending = PendingIntent.getBroadcast(context, 0, launchAppAction, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT); } else { launchAppActionPending = PendingIntent.getBroadcast(context, 0, launchAppAction, PendingIntent.FLAG_UPDATE_CURRENT); } – sejn Apr 13 '22 at 04:47
  • 3
    `PendingIntent.FLAG_MUTABLE` is not available for API levels below 31. Can't understand why this answer is the accepted one. – Shahood ul Hassan Oct 13 '22 at 06:49
-3

You can simply ignore the warning: on APIs levels under 23, the flag will simply be ignored (tested on API 21).

Max
  • 739
  • 2
  • 8
  • 23