Dagger: Proper Way To Define Injectable Class That Takes Context In Its Constructor
I want to use dagger (dagger v1 by Square) to create a singleton class whose constructor requires context as an argument. I then want to inject this singleton class into my MainAct
Solution 1:
You need to add the @ForApplication qualifier to your Context parameter in the SingletonClass constructor.
Dagger is now looking for a Context to inject, but only has a @ForApplication Context, which is a mismatch.
@Singleton
public class SingletonClass {
@Inject
SingletonClass(@ForApplication Context ctx) {
}
}
Now you can also get rid of the library = true line in your AndroidModule, which you've probably added because Dagger warned you that @ForApplication Context was unused (Don't ignore these warnings!).
Also, and that might just be a copy-paste error, this SingletonClass should not have an @Module annotation.
Post a Comment for "Dagger: Proper Way To Define Injectable Class That Takes Context In Its Constructor"