title: How to create custom scope and share same instances using Dagger Android
body: <p>So here are the things I know from the <a href="https://google.github.io/dagger/subcomponents" rel="nofollow noreferrer">doc</a></p>
  
  <ol>
  <li>Dagger Android under the hood is creating subcomponent for each Activity annotated with <code>ContributesAndroidInjector</code></li>
  <li>You can apply custom scope to the method where <code>ContributesAndroidInjector</code> is annotated to</li>
  <li><strong>If two sibling subcomponents have the same scope, they will still have different scope instances</strong></li>
  <li>If an Activity is in a subcomponent, it can have its own subcomponent which can contain Fragments. Those Fragments will share the scoped instances the Activity has.</li>
  </ol>
  
  <p>Now my question is:
  How to have one Activity be a subcomponent of another activity using Dagger Android?</p>
  
  <p>I want to do this because I want to achieve things like @UserScope/@SessionScope. </p>
  
  <p>From <a href="http://frogermcs.github.io/dependency-injection-with-dagger-2-custom-scopes/" rel="nofollow noreferrer">this</a> I know that I can do it with just Dagger not Dagger Android. But with Dagger Android, you can only have the Application (which is the AndroidInjector) to inject Activity. You can not have an Activity used as a holder or host of the parent subcomponent to inject another Activity.</p>
  
  <p>Am I understanding it correctly? </p>
  
  <p><strong>05/14/2018 Update:</strong></p>
  
  <p>I ended up getting rid of Dagger Android. So no more <code>ContributesAndroidInjector</code>, just pure Dagger. And to inject Activity/Fragment, I use the way that's recommended <a href="https://medium.com/@Zhuinden/that-missing-guide-how-to-use-dagger2-ef116fbea97" rel="nofollow noreferrer">here</a>. It will be something like this:</p>
  
  <pre><code>class MyActivity : AppCompatActivity() {
      private val factory: ViewModelProvider.Factory = Injector.myCustomScope().factory()
  }
  </code></pre>
  
  <p>And we are trying to make sure the factory is the only thing that Activity/Fragment needs.</p>
  
  <p>So far it's been great.</p>