Hey all. I use koin v2 and need to inject context....
# koin
n
Hey all. I use koin v2 and need to inject context. Any help? New to di
v
Consider
androidContext()
like someone else already mentioned to inject android context. Or using
parameterOf(this)
where
this
is the activity context
l
When starting koin in your Application class you define android context that Koin will use:
startKoin { androidContext(this@App) }
Then in your module definitions you can use
androidContext()
method to obtain it. If you need to inject activity context somewhere you can use something like this to define it:
single { (activity: AppCompatActivity) -> SomeDefinition(activity) }
and then call it like this:
val someInstance: SomeInstance by inject { parametersOf(this) }
👏 1