title: Dagger 2 injection in Fragments and swapping Modules
body: <p>I've started using Dagger 2 and I'm not sure if I'm doing things right as I ran in the following complication:</p>
  
  <p>Let's say I have a <code>HouseModule</code> initialized with a <code>House</code> and a <code>WindowModule</code> initialized with a <code>Window</code>.</p>
  
  <p>Now I have a <code>HouseFragment</code> which is supposed to general Information about the house.
  Thus I created a <code>HouseComponent</code> including the <code>HouseModule</code>.</p>
  
  <p>So far so good.
  Now there are multiple nested <code>HouseDetailsFragment</code>s within the <code>HouseFragment</code>(ViewPager) which show information about the <code>House</code> and the <code>Window</code>.
  I created a <code>HouseDetailsComponent</code> including the <code>HouseModule</code> and the <code>WindowModule</code>.</p>
  
  <p>My dependency graph looks like this:</p>
  
  <pre><code>// provides application wide dependencies (Application context, SharedPref, Repository,...)
  @Component(modules = {ApplicationModule.class}) 
  public interface ApplicationComponent { 
  
  }
  
  // provides general activity dependencies (Navigator,... )
  @Component(dependencies = ApplicationComponent, modules = {ActivityComponent.class}) 
  public interface ActivityComponent{
      void inject(MainActivity mainActivity); 
  }
  
  // provides house fragment specific dependencies (HousePresenter,...)
  @Component(dependencies = ApplicationComponent, modules = {ActivityComponent.class, HouseComponent.class}) 
  public interface HouseComponent extends ActivityComponent {
      void inject(HouseFragment fragment);  
  }
  
  // provides house details fragment specific dependencies (HouseDetailsPresenter,...)
  @Component(dependencies = ApplicationComponent, modules = {ActivityComponent.class, HouseComponent.class, WindowComponent.class}) 
  public interface HouseDetailsComponent extends ActivityComponent {
       void inject(HouseDetailsFragment detailsFragment);  
  }
  </code></pre>
  
  <p>Component creation becomes increasingly complex this way and I wonder how to inject dependencies to the <code>HouseFragment</code> and especially the <code>HouseDetailsFragment</code>s best.
  e.g. to build the <code>HouseDetailsComponent</code> I have to do the following:</p>
  
  <pre><code>// casting omitted
  DaggerHouseDetailsComponent.builder()
  .applicationComponent(getActivity().getApplication().getApplicationComponent())
  .activityModule(getActivity().getActivityModule())
  .houseModule(getParentFragment().getHouseModule())
  .windowModule(new WindowModule(window)).build().inject(this);
  </code></pre>
  
  <p>I dislike the knowledge (and casting) required about parent fragments and activities.
  Is there a simpler way to achieve what I want to do? Any other suggestions about the dependency graph?</p>
  
  <p>Also how do I show a different <code>House</code> in the <code>HouseFragment</code>? i figured creating a new <code>HouseModule</code> and then swapping the other out... but how do I do that? There are no accessors afaik.</p>