View dependency injection with dagger 2

前端 未结 3 934
伪装坚强ぢ
伪装坚强ぢ 2021-02-09 05:09

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

component.inject(customTextView);
3条回答
  •  遇见更好的自我
    2021-02-09 05:54

    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);
       }
    }
    

提交回复
热议问题