Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 891
Jeevan Rupacha Avatar asked Nov 14 '25 17:11

Jeevan Rupacha


1 Answers

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.

like image 138
Marcel Avatar answered Nov 17 '25 08:11

Marcel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!