Ok so I declared a lateinit var job
in a widget as below.
class TempHumidDisplayWidget : AppWidgetProvider(), CoroutineScope {
private lateinit var job: Job
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + job
...
In the onEnabled function, i initialized it:
override fun onEnabled(context: Context) {
job = Job()
Log.i("com.github.animeshz", "On enabled")
...
And inside the onUpdate function I am going to use launch which calls the get function of coroutineContext for dispatching a coroutine, which uses the job variable.
override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
// There may be multiple widgets active, so update all of them
for (appWidgetId in appWidgetIds) {
launch { updateAppWidget(context, appWidgetManager, appWidgetId) }
}
}
And inside the logcat, I am getting the following error:
4425-4425/? I/com.github.animeshz: On enabled
...
2020-05-01 11:06:06.639 4425-4425/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.github.animeshz.shivamwidget, PID: 4425
java.lang.RuntimeException: Unable to start receiver com.github.animeshz.shivamwidget.TempHumidDisplayWidget: kotlin.UninitializedPropertyAccessException: lateinit property job has not been initialized
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3183)
at android.app.ActivityThread.-wrap18(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1636)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6334)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property job has not been initialized
at com.github.animeshz.shivamwidget.TempHumidDisplayWidget.getCoroutineContext(TempHumidDisplayWidget.kt:26)
at kotlinx.coroutines.CoroutineContextKt.newCoroutineContext(CoroutineContext.kt:33)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:50)
at kotlinx.coroutines.BuildersKt.launch(Unknown Source)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:47)
at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source)
at com.github.animeshz.shivamwidget.TempHumidDisplayWidget.onUpdate(TempHumidDisplayWidget.kt:38)
at android.appwidget.AppWidgetProvider.onReceive(AppWidgetProvider.java:66)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3171)
at android.app.ActivityThread.-wrap18(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1636)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6334)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
It is clear that
onEnabled
is called before theonUpdate
method but thejob
variable is not initialized somehow, I don't know how to fix or debug it further
How is this possible that the job variable initialized in the onEnabled is actually not initialized in the onUpdate function? Is this a bug or something?
Any help would be appreciated, thanks in advance!
This is just spitballing, since I don't know details of widgets in Android, but something like this could work:
class TempHumidDisplayWidget : AppWidgetProvider() {
companion object {
val jobs = mutableMapOf<Context, Job>()
fun getScope(context: Context): CoroutineScope {
val job = jobs[context] ?: throw IllegalStateException("Context $context not enabled currently")
return object : CoroutineScope {
override val coroutineContext: CoroutineContext = Dispatchers.Main + job
...
}
}
}
override fun onEnabled(context: Context) {
jobs[context] = Job()
...
}
override fun onDisabled(context: Context) {
jobs.remove(context)?.cancel()
...
}
override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
val scope = getScope(context)
for (appWidgetId in appWidgetIds) {
scope.launch { updateAppWidget(context, appWidgetManager, appWidgetId) }
}
}
}
It's generally a bad idea to keep references to Context
s "statically", but this could be all right because they are removed in onDisabled
. Or maybe use WeakHashMap
instead.