question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inject using current Android Context

See original GitHub issue

Hello 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:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
arnaudgiulianicommented, Feb 21, 2018

Hello @vpotvin,

yes, there is no more AndroidModule. The applicationContext function opens a lambda to describe a Koin application description module (as said @caleb-allen)

You can use the androidApplication() function to inject your Android Application context instance where you need. i.e:

Given a class that needs your App context:

class MyRepository(application : Application)

You can write it like this:

val yourModule = applicationContext {
    bean { MyRepository(androidApplication())}
}

If you need Context type, you can directly write androidApplication().context

0reactions
vpotvincommented, Feb 23, 2018

Thank you for your help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dagger with Android: How do I inject the current context?
The only way to do this is to use a factory which allows you to specify the context with which the object will...
Read more >
Dependency injection in Android
The class constructs the dependency it needs. In the example above, Car would create and initialize its own instance of Engine . ·...
Read more >
Using Context | CodePath Android Cliffnotes
Overview. A Context provides access to information about the application state. It provides Activities, Fragments, and Services access to resource files, ...
Read more >
Dagger with Android: How to inject the current context - iTecNote
The only way to do this is to use a factory which allows you to specify the context with which the object will...
Read more >
Hilt by Android - Examples | Medium
Hilt - dependency injection library for Android. Examples from simple to advanced.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found