title: CustomView dependency injection with dagger 2 (within activity scope)
body: <p>My question is similar to <a href="https://stackoverflow.com/questions/36186085/view-dependency-injection-with-dagger-2">this</a>.</p>
  
  <p>So for instance, I have a <code>LiveData</code> implementation:</p>
  
  <pre><code>public class CustomLiveData extends LiveData&lt;SomeEvent&gt; {
  
      @Inject
      public CustomLiveData(@ActivityContext Context context) {
          //....
      }
  
  }
  </code></pre>
  
  <p>that I want to inject into a custom view:</p>
  
  <pre><code>public class CustomView extends View {
     @Inject
     SomeApplicationProvider anyProvider;
  
     @Inject
     CustomLiveData dataProvider; 
     // Getting @com.di.qualifiers.ActivityContext  android.content.Context cannot be provided without an @Provides-annotated method. 
     // @com.di.qualifiers.ActivityContext android.content.Context is injected at com.repositories.CustomLiveData.&lt;init&gt;(context)
     // com.repositories.CustomLiveData is injected at com.ui.CustomView.dataProvider com.ui.CustomView is injected at 
     // com.di.ApplicationComponent.inject(view)
  
     public CustomView(Context context) { this(context, null); }
     public CustomView(Context AttributeSet attrs) { 
        super(context, attrs);
  
        // Works ok for application provider
        Application.getComponent(context).inject(this);
     }
  }
  </code></pre>
  
  <p>And here is the rest of DI classes:</p>
  
  <pre><code>@ApplicationScope
  @Component(
          modules = {AndroidInjectionModule.class,
                  ActivityBuilder.class
          })
  
  public interface ApplicationComponent extends AndroidInjector&lt;MyApp&gt; {
  
      void inject(MyApp application);
  
      void inject(CustomView view);
  
      @Component.Builder
      abstract class Builder extends AndroidInjector.Builder&lt;MyApp&gt; {
  
          public abstract ApplicationComponent build();
      }
  }
  
  @ActivityScope
  @Module (subcomponents = MainActivitySubcomponent.class)
  public abstract class ActivityBuilder {
  
      @Binds
      @IntoMap
      @ActivityKey(MainActivity.class)
      abstract AndroidInjector.Factory&lt;? extends Activity&gt;
      bindActivityInjectorFactory(MainActivitySubcomponent.Builder builder);
  
      //...
  
  }
  
  
  @Subcomponent(modules = {MainActivityModule.class})
  public interface MainActivitySubcomponent extends AndroidInjector&lt;MainActivity&gt; {
  
      @Subcomponent.Builder
      abstract class Builder extends AndroidInjector.Builder&lt;MainActivity&gt; {
  
      }
  }
  
  @ActivityScope
  @Module
  public class MainActivityModule {
  
      @Provides
      @ActivityContext
      public Context provideActivityContext(MainActivity activity) {
          return activity;
      }
  
      // Seems to be wrong or not enough!?
      @Provides
      public CustomLiveData provideCustomLiveData(@ActivityContext Context context) {
          return new CustomLiveData(context);
      }
  }
  
  
  @Qualifier
  public @interface ActivityContext{
  }
  </code></pre>
  
  <p>Note, that I don't get any compiler complaints if <code>CustomLiveData</code> is injected into <code>MainActivity</code> instead into the view.
  Thanks!</p>