3

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)
user3728425
  • 183
  • 7
  • Your question asks about `PendingIntent.getActivity()` but your code example is for `PendingIntent.getService()`. Also, what do you mean by "it doesn't work". What doesn't work? What do you expect to happen? What do you see? Please clarify. – David Wasser Dec 08 '21 at 12:21

2 Answers2

0

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.

warbi
  • 2,725
  • 2
  • 20
  • 26
0

Faced similar issue after changing intents to use FLAG_UPDATE_CURRENT | FLAG_IMMUTABLE as per question. Then actually noticed that doc says specify either FLAG_IMMUTABLE or FLAG_MUTABLE. First one didn't work for me to pass data, switching to FLAG_MUTABLE seems to work:

    val flags = when {
        Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
        else -> PendingIntent.FLAG_UPDATE_CURRENT
    }
    val pendingIntent = PendingIntent.getBroadcast(ctx, someId.toInt(), intent, flags)
khusrav
  • 5,267
  • 5
  • 27
  • 38