Reputation: 5806
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 ?
Upvotes: 4
Views: 2413
Reputation: 2184
Update: since Glance beta01, recomposition is supported. Thus you can now consume flows/livedata and run side effects inside your composables. Although those will only be active while your process is alive.
Outdated below:
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.
Upvotes: 3