View dependency injection with dagger 2

会有一股神秘感。 提交于 2020-01-12 07:49:21

问题


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

component.inject(customTextView);

回答1:


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



回答2:


My general solution for that kind of thing is this

public class WelcomeView
        extends LinearLayout {
    private static final String TAG = "WelcomeView";

    public WelcomeView(Context context) {
        super(context);
        init(context);
    }

    public WelcomeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public WelcomeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @TargetApi(21)
    public WelcomeView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
        if(!isInEditMode()) {
            Application.getComponent(context).inject(this);
        }
    }



回答3:


UPDATE: Since 2.10 version of Dagger this answer is invalid.

Custom view:

public class CustomView extends View {
    @Inject
    AnyObject anyObject;

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        MainActivity mainActivity = (MainActivity) getContext();
        mainActivity.getComponent().inject(CustomView.this);
    }
}

Activity:

ActivityComponent mComponent;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mComponent = DaggerActivityComponent.builder()
                    .appComponent(getApp().getAppComponent())
                    .activityModule(new ActivityModule(MainActivity.this))
                    .build();
    mComponent.inject(MainActivity.this);
    ...
}

public ActivityComponent getComponent() {
    return mComponent;
}

Dagger2 component with Activity scope:

@ActivityScope
@Component(dependencies = AppComponent.class, modules = {ActivityModule.class})
public interface ActivityComponent extends AppComponent {

    void inject(MainActivity mainActivity);
    void inject(CustomView customView);
}

Scope:

@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface ActivityScope {}


来源:https://stackoverflow.com/questions/36186085/view-dependency-injection-with-dagger-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!