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

Example

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

 
class App : Application(), KodeinAware {

   override val kodein = Kodein {
     import(androidXContextTranslators)
     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)
       extend(appKodein)
       // 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 *