Inject using current Android Context
See original GitHub issueHello I’m wondering simply if there is a way to inject an Activity or while using either it’s context or the Application object to initialize the object.
So something like:
class HomeActivity : Activity() {
override val contextAwareObject by inject<ThisObjectNeedsContextInConstructor>()
}
I’ve tried using get()
for the context in a module but it doesn’t seem to be able to do the trick since it only has access to a Koin Context, not an Android one. I have seen an example using an AndroidModule
but it seems I can’t find an AndroidModule
in the current version.
In particular I’m trying to inject access to my database, which is currently initialized and owned by the Application class. It needs an App context to start. So therefore my Activities have to know about it even though I’m using MVP. It would be really nice if when I inject my presenter I could also inject the presenter by constructor with the database object.
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (3 by maintainers)
Hello @vpotvin,
yes, there is no more
AndroidModule
. TheapplicationContext
function opens a lambda to describe a Koin application description module (as said @caleb-allen)You can use the
androidApplication()
function to inject your AndroidApplication
context instance where you need. i.e:Given a class that needs your App context:
You can write it like this:
If you need
Context
type, you can directly writeandroidApplication().context
Thank you for your help!