The parameter is null when FLAG_IMMUTABLE is set in PendingIntent

Viewed 188

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)
2 Answers

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)
Related