I am using context()
in my GlanceAppWidget()
for tasks like retrieving glanceId and updating app widget state. I am having issue with how I inject the context
object.
I would like to use the dagger/hilt framework to inject the context into my GlanceAppWidget()
constructor. See MyWidget()
below.
However by injecting the context into MyWidget
, I then need to pass the context as constructor parameter in MyWidgetReceiver()
for val glanceAppWidget
. Broadcast receivers are not meant to have constructor arguments so this gives me an Instantiation Exception.
How can I inject context into my GlanceAppWidget? Any help will be much appreciated.
Note: I have also tried using default arguments in MyWidget()
to avoid providing context in MyWidgetReceiver but this throws "Type may only contain one injected constructor".
@Singleton
class MyWidget @Inject constructor(
@ApplicationContext val context: Context
) : GlanceAppWidget()
@AndroidEntryPoint
@Singleton
class MyWidgetReceiver @Inject constructor(
@ApplicationContext val context: Context /*<-java.lang.InstantiationException when trying to inject into BroadcastReceiver*/
) : GlanceAppWidgetReceiver() {
override val glanceAppWidget: GlanceAppWidget
get() = MyWidget(context)
}