android - Dagger 2 injection in Fragments and swapping Modules -


i've started using dagger 2 , i'm not sure if i'm doing things right ran in following complication:

let's have housemodule initialized house , windowmodule initialized window.

now have housefragment supposed general information house. created housecomponent including housemodule.

so far good. there multiple nested housedetailsfragments within housefragment(viewpager) show information house , window. created housedetailscomponent including housemodule , windowmodule.

my dependency graph looks this:

// 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);   } 

component creation becomes increasingly complex way , wonder how inject dependencies housefragment , housedetailsfragments best. e.g. build housedetailscomponent have following:

// casting omitted daggerhousedetailscomponent.builder() .applicationcomponent(getactivity().getapplication().getapplicationcomponent()) .activitymodule(getactivity().getactivitymodule()) .housemodule(getparentfragment().gethousemodule()) .windowmodule(new windowmodule(window)).build().inject(this); 

i dislike knowledge (and casting) required parent fragments , activities. there simpler way achieve want do? other suggestions dependency graph?

also how show different house in housefragment? figured creating new housemodule , swapping other out... how do that? there no accessors afaik.

i've been using submodules simplify hierarchy.

also dagger 2.11+ supports android , makes injections easier.


Comments