title: How to inject same Dagger 2 dependency with different implementations based on the Context?
body: <p>I have a Dagger 2 dependency that is provided differently if it’s an <code>Activity</code> or some other context instance (e.g., a <code>Service</code>). Both would refer to the same interface but their implementation varies. How could I organise this using Dagger 2?</p>
  
  <p>Right now, I’m trying with two different components, <code>ActivityComponent</code> and <code>ContextComponent</code> with their respective modules as follows:</p>
  
  <pre><code>@ActivityScope        
  @Subcomponent(            
    modules = {                
      ActivityModule.class,                
      ContextModule.class
    })        
  public interface ActivityComponent {
  }
  
  @Module    
  public class ActivityModule {
  
      @Provides
      @MyActivityQualifier
      public MyObject provideMyObject() {
      }
  }
  
  
  @ContextScope
  @Subcomponent(        
    modules = {
      ContextModule.class
    })    
  public interface ContextComponent {    }
  
  @Module
  public class ContextModule.class {
  
      @Provides
      public MyObject provideMyObject() {
      }
  } 
  </code></pre>
  
  <p>Then, if I’m using <code>MyObject</code> in an <code>Activity</code>, I have to add the qualifier as follows:</p>
  
  <pre><code>@Inject @MyActivityQualifier MyObject myObject; 
  </code></pre>
  
  <p>This feels wrong but I’m not sure why. Is there a better way?</p>