Home > Software engineering >  Android 12 Kotlin - Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functional
Android 12 Kotlin - Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functional

Time:05-05

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?

CodePudding user response:

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
    )
  • Related