Avatar ·

PendingIntent error: FLAG_IMMUTABLE or FLAG_MUTABLE required for Android 12+

Hello everyone! I'm a beginner in Android development with Kotlin, and I've encountered a problem when upgrading to targetSdk 31 (Android 12). I'm using MediaSessionCompat to control the media player. When creating a PendingIntent for the "Play/Pause" button in the notification, I get this error:\n\nTargeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.\n\nMy code looks like this:\n\n```kotlin\nval intent = Intent(this, MainActivity::class.java)\nval pendingIntent = PendingIntent.getActivity(this, 0, intent, 0)\n\nval playAction = NotificationCompat.Action(\n R.drawable.ic_play,\n "Play",\n pendingIntent\n)\n```\n\nI understand that I need to add a flag, but I'm not sure which one to choose and how it will affect the behavior of notifications.