1

How do you provide dependencies to your views (android.view.View)? Most examples I've seen in dagger provide them through the view's context, but that doesn't seem very composable. Say if I have a two-pane view, I'd like the enclosing view to provide the dependencies to the master and detail views. If use the context from the nested views, the depenedencies will still be provided by the activity and not the enclosing view. One solution would be to wrap the context passed to the nested view, but that will make inflation difficult. This is not specifically a dagger question.

3
  • What is the use case where you want to inject something into a view and then pass that dependency to a child view rather than injecting the dependency directly into the child view? Oct 21, 2015 at 14:57
  • The view operates on it's parent in some way. If the callback is passed through Context, it will operate on the activity (unless ContextWrapper). Passing callbacks via a method is not as fun (no final)
    – DariusL
    Oct 21, 2015 at 16:40
  • A want to inject a DI in a customview (like CustomTextView extends TextView), where should I call inject? Mar 23, 2016 at 18:30

2 Answers 2

0

If I understood you correctly, you want to pass some dependency to your View. As far as I know, you can inject dependencies in the same way as in the Activity. With Dagger 2, you can define component, module, interface and use @Inject annotation. It should work. I actually haven't used injections in Views. I usually use them in Activities and just pass some data (like String, Integer, List of objects, custom object etc.) to the View. I think, views should be as simple as possible and I'm not sure if using injections inside them is the right thing. Maybe it could be right in a very specific solution.

2
  • I understand how to inject, I'm asking if there's a way for the enclosing view to provide the dependencies for the nested view.
    – DariusL
    Oct 20, 2015 at 9:15
  • I think, the easiest way to find out is to try it. I don't know if you can pass injections from enclosing view to the nested view, but you can try to initialize injections in the nested view. Optionally, you can initialize injections in a shared object, which can be used by enclosing and nested view. Maybe it could be good idea to ask this question via issue on GitHub, so Dagger maintainers can give you better answer. Oct 20, 2015 at 9:27
0

You could just do a blind traversal up the view tree, asking each parent if it has an injector it should use.

Alternatively, you could have the enclosing view be responsible for the injections, rather than have the nested views attempt to inject themselves. After all, even though the example guides for dagger show objects injecting themselves, dependency injection in general is supposed to be the other way around: something else injects into us.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.