I ended up doing something like that, but I doubt ...
# kodein
k
I ended up doing something like that, but I doubt it's the idiomatic way…
Copy code
class BatteryBroadcastReceiver : KodeinAware, BroadcastReceiver() {

    override lateinit var kodein: Kodein

    private val myBinding: Stuff by instance()

    override fun onReceive(context: Context?, intent: Intent?) {
        if (!::kodein.isInitialized) {
            kodein = (context as KodeinAware).kodein
        }
        ...
    }
}
e
I do it this way. However, I'd suggest context.applicationContext because depending on how the receiver is registered, the incoming Context may not be KodeinAware. If the receiver is not registered via the manifest but is, for example, registered in a non-KodeinAware Activity, the cast will fail. This may not be the case for you nor I, but it's the safer alternative as it only relies on the Application instance being KodeinAware.