'How to make live update of glance widget?

Is that possible to make live update the data of Widget using Glance jetpack compose ? Like update data while database is changes . For example while I am using flow so flow should be able to update the widget data right?

We can update on Click action like :

class MyWidgetActions: ActionCallback {


    override suspend fun onRun(context: Context, glanceId: GlanceId, parameters: ActionParameters) {
                updateAppWidgetState(context, PreferencesGlanceStateDefinition, glanceId) {
                it.toMutablePreferences()
                     .apply {
                       ...
                       //toDoSomething()
                       ... 
                    }
                }
                MyWidget().update(context, glanceId)
             ...
    }

Can we update without on click with flow of data ?



Solution 1:[1]

You can update the widget instance from anywhere of your app by using one of the update methods, but you cannot subscribe or observe any flow/livedata inside the Widget Content/Composables methods.

Although that means that your app must be running, so you would need a service running. We don't recommend to have "live" widgets updating constantly since it could cause battery drain.

Rather use WorkManager to schedule periodic updates.

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 Marcel