Is there a way to bind a dependency twice? Once in...
# dagger
c
Is there a way to bind a dependency twice? Once in SingletonComponent, once in ActivityComponent? In my use case I have two instances of a dependency: once in an Activity and once in a Service. The dependency itself depends on a
Context
, so I want to use the Activity context where possible. I suppose I could accomplish this binding with annotations, but I was wondering if there was another way.
1
f
You can use assisted inject for this and have the context as assisted dependency
w
IMO appropriate way to provide the same dependency twice in different scopes is to use qualifiers. Otherwise how would you decide which one to inject, if both are available?
c
Thanks for responding. In the end, because I needed one in an Activity and one in a Service, I put one in ActivityComponent and the other in ServiceComponent, so there was no collision.