How to apply Dagger injection into services?
See original GitHub issueFragment & Activity injection is done through registerActivityLifecycleCallbacks
and registerFragmentLifecycleCallbacks
, but it’s unclear how to turn on injection for Services
.
Any ideas?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:6
- Comments:8 (1 by maintainers)
Top Results From Across the Web
Using Dagger 2 to inject into service - android - Stack Overflow
2 Answers 2 · Declare a component, · add the inject method to that component, · add a module providing your service ·...
Read more >Using Dagger in Android apps
To inject an object in the activity, you'd use the appComponent defined in your Application class and call the inject() method, passing in...
Read more >Using Dagger 2 for dependency injection in Android - Tutorial
Dagger 2 is dependency injection framework. It is based on the Java Specification Request (JSR) 330. It uses code generation and is based...
Read more >Dependency Injection with Dagger 2 - CodePath Cliffnotes
Instantiating the component ... Make sure to rebuild the project (in Android Studio, select Build > Rebuild Project) if you cannot reference the...
Read more >Dependency injection with Dagger 2: @Inject and @Provides
The @Inject annotation is concise and easy to use. However, there are cases where this annotation cannot be used: ... In this case,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Here’s my solution:
Make Application implements HasServiceInjector and inject a DispatchingAndroidInjector for services.
Create a new module to perform injection over your services.
Register your new module in your application’s component.
And finally, override method onCreate of the service adding AndroidInjection.inject(this).
Ok, but what about integration tests? It will crash since Github example app is using different
Application
class which disables Dagger initialization and you are responsible for manually providing mock-objects into properties.