Home > Back-end >  Missing mutability flags: Android 12 pending intents with NavDeepLinkBuilder
Missing mutability flags: Android 12 pending intents with NavDeepLinkBuilder

Time:11-04

I´ve started testing my app for issues on Android 12, and have had some warnings regarding mutability flags on pending intents that are set up for a home screen widget. These mutability flags are now mandatory from SDK 31. Before 31, this code would work but with a lint warning:

val myIntent = Intent(context, MainActivity::class.java)
val myPendingIntent = PendingIntent.getActivity(context, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT )
myRemoteViews.setOnClickPendingIntent(R.id.widget_button, myPendingIntent)

On Android 12, it is necessary to add a second flag, like so:

val myIntent = Intent(context, MainActivity::class.java)
val myPendingIntent = PendingIntent.getActivity(context, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
myRemoteViews.setOnClickPendingIntent(R.id.widget_button, myPendingIntent)

So far so good. However, for some of the widget actions it is necessary to navigate not only into the app, but to a specific fragment and with fragment arguments added. For these cases I´ve been using NavDeepLinkBuilder to create the pendingIntent https://developer.android.google.cn/reference/kotlin/androidx/navigation/NavDeepLinkBuilder

val myPendingIntent = NavDeepLinkBuilder(context)
        .setGraph(R.navigation.nav_main)
        .setDestination(R.id.myFragment)
        .setArguments(
            myFragment.bundle(myArgs)
        )
        .createPendingIntent()
myRemoteViews.setOnClickPendingIntent(R.id.widget_button, myPendingIntent)

This works great until you set build target to sdk 31 and build on Android 12 (emulator). In that case it crashes upon widget creation with this stacktrace:

   2021-11-03 17:41:27.792 20309-20309/com.package.debug E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.package.debug, PID: 20309
    java.lang.RuntimeException: Unable to start receiver com.package.widgets.mywidget.ui.MyWidgetProvider: java.lang.IllegalArgumentException: com.package.debug: 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.
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:4321)
        at android.app.ActivityThread.access$1600(ActivityThread.java:247)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2068)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7842)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
     Caused by: java.lang.IllegalArgumentException: com.package.debug: 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.
        at android.app.PendingIntent.checkFlags(PendingIntent.java:375)
        at android.app.PendingIntent.getActivitiesAsUser(PendingIntent.java:593)
        at android.app.PendingIntent.getActivities(PendingIntent.java:575)
        at androidx.core.app.TaskStackBuilder.getPendingIntent(TaskStackBuilder.java:341)
        at androidx.core.app.TaskStackBuilder.getPendingIntent(TaskStackBuilder.java:312)
        at androidx.navigation.NavDeepLinkBuilder.createPendingIntent(NavDeepLinkBuilder.java:260)
        at com.ms1881.widgets.listwidget.ui.ListWidgetProvider.buildRemoteViews(ListWidgetProvider.kt:91)
        at com.ms1881.widgets.listwidget.ui.ListWidgetProvider.updateListWidget(ListWidgetProvider.kt:44)
        at com.ms1881.widgets.listwidget.ui.ListWidgetProvider.onUpdate(ListWidgetProvider.kt:39)
        at android.appwidget.AppWidgetProvider.onReceive(AppWidgetProvider.java:66)
        at com.ms1881.widgets.listwidget.ui.ListWidgetProvider.onReceive(ListWidgetProvider.kt:169)
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:4312)
            ... 9 more

So the question is: How can I set the mandatory mutability flags when using NavDeepLinkBuilder to build pending intents? I cannot find any info on this in the docs on NavDeepLinkBuilder. Setting up the pending intents as in the second code snippet is possible, and I can use an intent action or extra to set an "address" to get to the desired fragment, but I lose the possibility of sending a bundle argument to the fragment itself. Appreciate all inputs on how to solve this.

CodePudding user response:

As per the Navigation 2.4.0-alpha04 release notes:

NavDeepLinkBuilder now adds PendingIntent.FLAG_IMMUTABLE to the PendingIntent returned by createPendingIntent(), ensuring that this API works as expected when targeting Android 12. (If8c52)

You'll need to upgrade to Navigation 2.4.0-alpha04 or higher (currently 2.4.0-beta01) if you want that fix.

  • Related