Search code examples
androidandroid-widgetsamsung-mobileandroid-8.0-oreo

Android requestPinAppWidget returns false after recent installation


I'm having issues with AppWidgetManager::requestPinAppWidget

Sometimes the method requestPinAppWidget returns false. We reproduce it by uninstalling the app and installing again from fresh using android studio. We can consistently reproduce it on Samsung and Pixel devices using this code:

@RequiresApi(Build.VERSION_CODES.O)
private fun showWidgetAddingActivity(context: Context) {
    val appWidgetManager = context.applicationContext.getSystemService(AppWidgetManager::class.java)
    val myProvider = ComponentName(context.applicationContext, WidgetActionsReceiver::class.java)

    val launcherSupported = appWidgetManager.requestPinAppWidget(myProvider,null, null)
}

the value of launcherSupported will be false if we run this after a fresh installation (or within a few minutes afterward). And it will be true in any other case

Calling appWidgetManager.isRequestPinAppWidgetSupported always returns true when this happens.

Why is that? How can I mitigate it? Or at least know about it in advance so I won't show the user the rest of the flow


Solution

  • This was caused by WidgetActionsReceiver being disabled by default. When it was enabled using a feature flag, it took time for the change to reach the launcher.