'Intent from widget not taken into account since Android SDK 31

My app offers the possibility to launch it through as many widgets as wanted by the user. To determine which widget trigerred the launch, I use an extra parameter in the Intent associated to the widget. This parameter is the widget id provided by the update function of my AppWidgetProvider. As long as I target SDK version up to 30, everything is fine. If I try to target to Android SDK 31 or 32, when I add a new widget, I get the following error :

java.lang.IllegalArgumentException: vd63.utilitaires.memo: 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.

I have tried to declare my Intent mutable, as Intents were until now, as far as I have read, mutable by default. But when I click on the widget, the app reappears but doesn't take into account the Intent passed to the widget. I tried to change the Intent to immutable, but without improvements.

Does anyone know the origin of this change in behaviour between Android SDK 31 and it's predecessors, and how to cope with it?

Edit : targetting SDK 30 doesn't remove the problem when the app is running on a device whose OS is Android 12. Targetting SDK 32 is neither a problem when running on an Android 8 device.



Solution 1:[1]

This issue came since sdk 31 currently i am handling it with this condition in my project.

Kotlin solution

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT else PendingIntent.FLAG_UPDATE_CURRENT

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 Mahek Thaker