我正在为我正在开发的一个简单的待办事项列表应用程序实现我的第一个 Jetpack Glance 支持的应用程序小部件。我按照https://developer.android.com/jetpack/compose/glance上的指南进行操作,一切都很好,直到我需要更新我的小部件以匹配应用程序内发生的数据更新。
根据我对管理和更新 GlanceAppWidget的理解,可以通过从应用程序代码本身调用小部件实例上的update、updateIf或方法来触发小部件重组。updateAll具体来说,对这些函数的调用应该触发该GlanceAppWidget.provideGlance(context: Context, id: GlanceId)方法,该方法负责获取任何所需的数据并提供小部件内容,如以下代码片段中所述:
class MyAppWidget : GlanceAppWidget() {
override suspend fun provideGlance(context: Context, id: GlanceId) {
// In this method, load data needed to render the AppWidget.
// Use `withContext` to switch to another thread for long running
// operations.
provideContent {
// create your AppWidget here
Text("Hello World")
}
}
}
Run Code Online (Sandbox Code Playgroud)
但就我而言,它并不总是有效。以下是我在几次尝试后观察到的结果:
updateAll)。小部件已更新并显示最新数据,provideGlance方法根本不会被触发。然后我查看了GlanceAppWidget …