Skip to content
This repository has been archived by the owner on Aug 26, 2021. It is now read-only.

Example for field injection into View classes #382

Closed
christopherperry opened this issue Jan 24, 2014 · 3 comments
Closed

Example for field injection into View classes #382

christopherperry opened this issue Jan 24, 2014 · 3 comments

Comments

@christopherperry
Copy link

I'm in the middle of refactoring our code base from using Roboguice and I've hit a problem I'm not sure how to solve in an elegant way. We have all these places where people on my team used @Inject to inject into fields on custom Views that were inflated via xml, and Roboguice was magically resolving these. Now I'm trying to deal with these using Dagger.

How do you deal with these in Dagger? I'd hate to grab the activity graph with something like:

((DaggerActivity) context).getActivityGraph().inject(this);

that makes me feel all dirty inside.

@JakeWharton
Copy link
Member

That's exactly what we do. You can also write a Factory for LayoutInflater and pass the ObjectGraph as an argument.

@christopherperry
Copy link
Author

@JakeWharton Which way has been used to greater success? I'd like to make sure I'm not bloating my unit tests to set up stuff, and since I'm using field injection (on Views) it forces Dagger into the mix which I'm really not digging. Not sure I have much choice otherwise as passing dependencies in a method would force me to inject them outside the View and pass them in which sort of sucks as well.

@JakeWharton
Copy link
Member

Both work equally fine. It's a matter of preference, I guess. Downside of the latter is that layout preview breaks.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants