4

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?

CookieMonsta
  • 53
  • 1
  • 4

1 Answers1

2

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
    )
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Android 12 includes important changes to pending intents, including a change that requires explicitly deciding when a PendingIntent is mutable or immutable. Read [this](https://medium.com/androiddevelopers/all-about-pendingintents-748c8eb8619) great article for detailed information – Muhammad Afzal Jun 13 '22 at 21:49
  • 1
    any PendingIntents created without the FLAG_IMMUTABLE flag were implicitly mutable – Muhammad Afzal Jun 13 '22 at 22:16