Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View dependency injection with dagger 2

I have a custom view extending a TextView. Where should I call my component to inject the view?

component.inject(customTextView);
like image 525
Sibelius Seraphini Avatar asked Mar 23 '16 18:03

Sibelius Seraphini


1 Answers

So, I've find out that I need to add the injection in the constructor of my custom view (in all of them, or make one call the other)

Example:

public class CustomTextView extends TextView {
   @Inject
   AnyProvider anyProvider;

   public CustomTextView(Context context) { this(context, null); }
   public CustomTextView(Context AttributeSet attrs) { 
      super(context, attrs);
      Application.getComponent(context).inject(this);
   }
}
like image 173
Sibelius Seraphini Avatar answered Nov 09 '22 23:11

Sibelius Seraphini