How to use Kodein with Conductor in Android?

177 Views Asked by At

I want to use both Conductor and Kodein in my application. The problem is that both the application context and the activity are null on the Controller setup and I can't access Kodein instance.

class SetupNavigationController: Controller(), KodeinAware {
    override val kodein: Kodein by closestKodein(applicationContext!!)
    //    val kodein = (activity as MainActivity).kodein
    val someInstance: SomeClass by kodein.instance()
}
1

There are 1 best solutions below

3
On

Kodein is lazy by default especially because the applicationContext is null at the time of class instanciation and set later by the system.

You need to use the lazy syntax, so that applicationContext will only be accessed when needded:

class SetupNavigationController: Controller(), KodeinAware {
    override val kodein: Kodein by kodein { applicationContext!! }
    val someInstance: SomeClass by kodein.instance()
}