1

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 Answers2

2

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.

Marcel
  • 2,094
  • 3
  • 23
  • 37
1

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)