Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 12 Kotlin - Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable

This is the code for pendingIntent:

val pi =
    PendingIntent.getActivity(
        applicationContext,
        0,
        ii,
        PendingIntent.FLAG_UPDATE_CURRENT
    )

I'm getting this error when using it:

java.lang.IllegalArgumentException: de.xxx.xxx: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.

I still need the activity to be updated so how can I add this FLAG_IMMUTABLE or FLAG_MUTABLE what ever the hell this is and still be able to update the activity? Based on this answer I tried:

val pi =
    PendingIntent.getActivity(
        applicationContext,
        0,
        ii,
        PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
    )

but this gives syntax error!

So how should the code look?

like image 337
CookieMonsta Avatar asked Nov 01 '25 13:11

CookieMonsta


1 Answers

Based on this answer

The code presently shown in that question and answer are in Java. You are writing in Kotlin.

In Kotlin, use the or bitwise operator:

val pi =
    PendingIntent.getActivity(
        applicationContext,
        0,
        ii,
        PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
    )
like image 100
CommonsWare Avatar answered Nov 04 '25 04:11

CommonsWare



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!