Issue
I'm not sure if this is a problem with Glance still being in alpha, or if I'm doing something wrong. But every time the widget receiver's onUpdate()
is triggered, it's being completely re-composed. It resets to the initialLayout
for a second, and then re-composes to the correct state.
The problem is that it happens even if there is no change in the widget state, so it just "flickers" like that every time there's a widget update, and it looks really bad.
I've already implemented manual updates using MyWidget().updateIf<Preferences>
, so that my app only updates the widget when the state changes, but there are still automatic updates that the OS is doing, so the "flickering" is still happening.
EDIT:
After some more testing, I found that this isn't actually happening when calling the GlanceAppWidget
update()
method. In fact, I removed all calls to the GlanceAppWidget
and GlanceAppWidgetManager
from my app. However, the "flickering" is still happening any time the widget gets updated (automatically, triggered by the OS).
I've tried disabling the widget refresh in the XML by setting the updatePeriodMillis
to both 0 and 86400000, but that doesn't seem to work. I've also tried removing the updatePeriodMillis
from the XML.
So, it appears that the flicker happens any time the GlanceAppWidget has it's Content() function called, regardless of what's actually triggering that call. Just for reference, here's the basic Kotlin class for the widget:
class WidgetSimple : GlanceAppWidget() {
override val sizeMode: SizeMode = SizeMode.Single
@Composable
override fun Content() {
// code to actually draw the components
// no matter what's here, the widget will flicker
// even if we leave it blank, it'll still flicker between the preview layout and a blank screen
}
}
class WidgetSimpleReceiver : GlanceAppWidgetReceiver() {
override val glanceAppWidget: GlanceAppWidget = WidgetSimple()
}
Solution
This isn't really a solution, but it may help someone else who's experiencing this issue...
This isn't actually a Glance problem -- it's some kind of an issue with WorkManager
. Basically, any time WorkManager.enqueueUniqueWork()
is triggered, the widget flickers. I just happened to use WorkManager in all of my Broadcast Receivers for background processing, which is how I stumbled onto this problem.
But I've tested this without using Glance at all (generating RemoteViews
and using AppWidgetManager
), and those widgets also flicker any time WorkManager.enqueueUniqueWork() is called
Answered By - user496854
Answer Checked By - Pedro (JavaFixing Volunteer)