Do I need to setup Modules, Providers and Interfaces (Components)?
Seems like quite a bit of overhead just to be able to inject a singleton.
Can someone provide a simple singleton example using Dagger 2? (also show how one can set properties of the singleton like the context so you don't need to pass it in every time you use the singleton)
Best Answer
You only need modules for things that you can't annotate with
@Inject
constructor (because for example, the framework creates it for you - like context). If you can't add an @Inject constructor, you also need to specify avoid inject(...)
method in the component as well.However, if you can create it with a
@Inject
constructor, then@Inject
works as a field annotation as wellYou can also do constructor parameters
Verified to work: