Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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
    )
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading