title: Dagger 2 on Android: inject same dependency in Activity and retained Fragment
body: <p>I have objects of classes <code>F1</code> and <code>F2</code> that I want to inject in a retained Fragment. I also have an object of class <code>A</code> that depends on Activity, and I want it to be injected in that Activity and in a retained Fragment attached to that Activity's Fragment Manager. I write the following code. First, the module for the Activity dependency:</p>
  
  <pre><code>@Module
  public class MainActivityModule {
      private Activity mActivity;
  
      public MainActivityModule(Activity activity) {
          mActivity = activity;
      }
  
      @Provides
      @ActivityScope
      public A provideA() {
          return new A(mActivity);
      }
  }
  </code></pre>
  
  <p>Then, the corresponding component, that must make the <code>A</code> object available to its dependent components:</p>
  
  <pre><code>@ActivityScope
  @Component(modules = {MainActivityModule.class})
  public interface MainActivityComponent {
      void inject(MainActivity activity);
  
      // make the A object available to dependent components
      A getA();
  }
  </code></pre>
  
  <p>I also write the Fragment-related module:</p>
  
  <pre><code>@Module
  public class FragmentModule {
      @Provides
      @FragmentScope
      public F1 provideF1() {
          return new F1();
      }
  
      @Provides
      @FragmentScope
      public F2 provideF2() {
          return new F2();
      }
  }
  </code></pre>
  
  <p>and its corresponding component:</p>
  
  <pre><code>@FragmentScope
  @Component(modules = {FragmentModule.class}, dependencies = {MainActivityComponent.class})
  public interface FragmentComponent {
      void inject(MyFragment presenter);
  }
  </code></pre>
  
  <p>Finally, I inject the dependency on <code>A</code> in the Activity, where I also need to call specific life cycle methods on it. The Activity also provides a method to get the component so that the Fragment is able to use it when building its own component:</p>
  
  <pre><code>// in MainActivity.onCreate
  mActivityComponent = DaggerMainActivityComponent.builder()
          .mainActivityModule(new MainActivityModule(this))
          .build();
  mActivityComponent.inject(this);
  mA.onCreate();
  </code></pre>
  
  <p>and I try to inject the dependencies on <code>A</code>, <code>F1</code>, <code>F2</code> in the Fragment, too:</p>
  
  <pre><code>// in MyFragment.onCreate
  FragmentComponent component = DaggerFragmentComponent.builder()
          .fragmentModule(new FragmentModule())
          .mainActivityComponent(((MainActivity) getActivity()).getComponent())
          .build();
  component.inject(this);
  </code></pre>
  
  <p>However, since the Fragment is retained, when the Activity is destroyed and recreated by the system reacting to a configuration change (e.g. a device rotation), the Fragment maintains a reference to the old <code>A</code> instance, while the new Activity has correctly recreated a new <code>A</code> instance to go with it. To work around this problem, I have to create the <code>FragmentComponent</code> and inject dependencies in <code>MyFragment.onActivityCreated</code> rather than <code>MyFragment.onCreate</code>. On the other hand, this implies that <code>F1</code> and <code>F2</code> dependencies are recreated every time the activity is destroyed and recreated; but they are Fragment-scoped dependencies, so they should follow the Fragment life cycle instead of the Activity's.</p>
  
  <p>Therefore, my question is as follows: is it possible to have differently-scoped dependencies injected in a retained Fragment? Ideally, <code>F1</code> and <code>F2</code> dependencies should be injected in <code>MyFragment.onCreate</code>, while <code>A</code> dependency should be injected in <code>MyFragment.onActivityCreated</code>. I tried using two different components, but it seems not to be possible to perform partial injection. Currently, I ended up adding an explicit reassignment of the Fragment <code>A</code> dependency in <code>MyFragment.onActivityCreated</code>, but that's not really injection, you know. Could this be done in a better way?</p>