Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing PendingIntent mutability flag giving warning after checking sdk version

Hey I am getting warning for pending intent. So I surrounded for check of checking sdk according to this question and this medium post. I am getting warning message Missing PendingIntent mutability flag

val pendingIntent: PendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
            } else {
                PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
            }

enter image description here

How can I remove this warning message?

enter image description here

like image 282
Vivek Modi Avatar asked Jun 28 '26 22:06

Vivek Modi


1 Answers

Your code seems OK, and I believe it's a bug in the Lint check as it's been
stated by @CommonsWare in comments. This could be fixed in the next releases of Android Studio

How can I remove this warning message?

If you just want to remove the annoying warning there is a hack in building the condition that clear the warning: by transferring the conditional to the flag:

val pendingIntent: PendingIntent = PendingIntent.getActivity(
    this,
    0,
    intent,
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
    else PendingIntent.FLAG_UPDATE_CURRENT
)

Or at the worse case you'd suppress it by @SuppressLint("UnspecifiedImmutableFlag"), which I don't recommend.

like image 66
Zain Avatar answered Jul 01 '26 13:07

Zain