Delete the Glance widget from home screen

709 views Asked by At

I would like delete my widget (Glance Jetpack) from the home screen, but unsuccesfull. My code is:

val scope = CoroutineScope(Dispatchers.Default)

scope.launch {
  val glanceId =GlanceAppWidgetManager(context).getGlanceIds(ActionWidget::class.java)
                                    .last()
val glanceAppWidget: GlanceAppWidget = ActionWidget()
glanceAppWidget.onDelete(context, glanceId)
}
2

There are 2 answers

0
Marcel On

It's not possible to remove a widget programatically. You are just manually calling the callback. The widget must be manually removed by the user unless the app removes the widget from the Android manifest.

You could set the android:enabled=false in your widget provider that should remove the widget once the app gets updated.

2
Dmitry Sergienko On

Many thanks for Marcel! I fixed the issue by following his advice! My code is:

val receiver = ComponentName(context, ActionWidgetReceiver::class.java)

val pm = context.packageManager

pm.setComponentEnabledSetting(
      receiver,
      PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
      PackageManager.DONT_KILL_APP)