'The parameter is null when FLAG_IMMUTABLE is set in PendingIntent

Currently, I was trying to use FLAG_IMMUTABLE for PendingIntent.getActivity(For android 12). However, it doesn't work (The parameter set in PendingIntent is null). If I replace it with FLAG_MUTABLE, it does work.

PendingIntent.getService(activity, 0, myServiceRequestIntent, PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_MUTABLE)

According to google docs, FLAG_IMMUTABLE is recommended way to pass pending intent. Are there some conditions or situations where you cannot use FLAG_IMMUTABLE? If there are examples of tips that will be really helpful

The additional_extra extra is null when I pass it back in the following format.

val pendingIntent = intent.getParcelableExtra("pending_intent")

        val intent = Intent().apply {
            putExtra("additional_extra", "test")
        }

        pendingIntent?.send(this, 0, intent)


Solution 1:[1]

Are there some conditions or situations where you cannot use FLAG_IMMUTABLE?

According to: https://developer.android.com/guide/components/intents-filters#CreateImmutablePendingIntents

...certain use cases require mutable PendingIntent objects instead

I have found for instance that BLE scanning using a PendingIntent and Activity Recognition updates to an IntentService both require FLAG_MUTABLE, otherwise the delivered intents are essentially empty.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 warbi