Vivek Modi
Vivek Modi

Reputation: 7281

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

Upvotes: 8

Views: 2455

Answers (2)

Zain
Zain

Reputation: 40878

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.

Upvotes: 3

Muhammad Asad
Muhammad Asad

Reputation: 668

Add pending intent like this

val updatedPendingIntent = PendingIntent.getActivity(
applicationContext,
NOTIFICATION_REQUEST_CODE,
updatedIntent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT )

If the error still remains and your targetSdkVersion = 31 then the error must caused because one of your dependencies is internally using WorkManager or your are directly using old version of WorkManager. To solve simply add this dependency

implementation 'androidx.work:work-runtime-ktx:2.7.0'

Upvotes: 2

Related Questions