11

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.mypackage.widgets.listwidget.ui.ListWidgetProvider.buildRemoteViews(ListWidgetProvider.kt:91)
        at com.mypackage.widgets.listwidget.ui.ListWidgetProvider.updateListWidget(ListWidgetProvider.kt:44)
        at com.mypackage.widgets.listwidget.ui.ListWidgetProvider.onUpdate(ListWidgetProvider.kt:39)
        at android.appwidget.AppWidgetProvider.onReceive(AppWidgetProvider.java:66)
        at com.mypackage.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.

Leknesh
  • 307
  • 2
  • 10

4 Answers4

15

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.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • I was using Navigation 2.3.5, didn't think about checking the release notes for changes on this issue. Thank you for input (and for the Navigation framework) – Leknesh Nov 03 '21 at 17:32
  • 2
    Navigation version 2.4.0+ includes multiple backstack. So, I cannot use this latest version. How can I fix this crash without updating to the latest version or how can I stop multiple backstack in the latest version? – Abdullah Javed Jan 19 '22 at 12:19
  • I'm also facing this problem, some bug from versions 2.4.0+ prevent me from bumping the dependency version. – Luis Ventura Feb 09 '22 at 07:27
6

In case, you're encountering a bug after upgrading to a newer version. You can create the PendingIntent like this:

NavDeepLinkBuilder(context)
    .createTaskStackBuilder()
    .getPendingIntent(
        RequestCode,
        PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
    )
Ali Albaali
  • 61
  • 2
  • 2
  • I'm using version 2.5.1 of Navigation and still getting this crash in my app. What could be the problem? – dimioLt Jan 16 '23 at 12:35
  • I'm still stuck in version 2.3.5. Because, I'm running into some errors when upgrading. – Ali Albaali Jan 18 '23 at 02:35
  • And what RequestCode do you use for your PendingIntent? Always 0? – dimioLt Jan 18 '23 at 12:00
  • I downgrade my version to 2.3.5 and tried to create the PendintIntent like in your answer and I still getting the same crash, what can be wrong? I don't understand. – dimioLt Jan 18 '23 at 12:23
0

Update Navigation related dependencies in your module-gradle file:

implementation 'androidx.navigation:navigation-ui-ktx:2.5.3'
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'
canerkaseler
  • 6,204
  • 45
  • 38
0

Worked for me.

I just upgrade from this

implementation("androidx.navigation:navigation-fragment-ktx:2.3.5") implementation("androidx.navigation:navigation-ui-ktx:2.3.5")

To this:

implementation("androidx.navigation:navigation-fragment-ktx:2.5.3")
implementation("androidx.navigation:navigation-ui-ktx:2.5.3")

This solved the pending intent issue that I'm having it on android 13.

Ammar
  • 765
  • 1
  • 8
  • 18