Kodein: Inherit parent dependencies

When you override Kodein instance in activity, fragment or somewhere else, you will lose all dependencies from parent scope.
To prevent this, there is a extend method


In my App I have settings instance I want to use in Activity

class App : Application(), KodeinAware {

   override val kodein = Kodein {
     bind<ISettings>() with singleton { Settings(this@App) }

Now in activity:

class MainActivity : Activity(), KodeinAware {

    private val appKodein by kodein(App.instance)

    override val kodein = Kodein {
       // or
       // extend(appApplication.appKodein(), allowOverride = true)
       // now you have all dependecies, you initialized in App

    private val settings: ISettings by instance() // here we have it. Able to use settings instance

No Comments

You can leave the first : )

Leave a Reply

Your email address will not be published. Required fields are marked *