https://kotlinlang.org logo
#glance
Title
# glance
p

Piotr Prus

03/08/2022, 1:13 PM
Hello, anyone here know if there is possibility to update the widget from
Worker
using
updateAppWidgetState
? I have tried to use it in Worker, but the update function need to be used from GlanceWidget class or I have to pass the
GlanceStateDefinition
. So the question is, can I create the State Definition inside my worker? If not, what is the way to update the widget from
Worker
? I found that passing my state in constructor of MyGlanceWidget works, but then I am not using widgetState at all.
1
I may have found a solution:
Copy code
val glanceId = GlanceAppWidgetManager(appContext).getGlanceIds(MyWidget::class.java).first()

MyWidget().apply {
    updateAppWidgetState(appContext, glanceId) { prefs ->
    // update the preferences, ex: prefs[myVariableKey] = true
    }
    // Manually trigger update of widget
    update(appContext, glanceId)
}
3 Views