android - How to use AndroidInjector with subcomponents -


i have following

applicationcomponent

  • subcomponentblue( modules= modulex.class)

  • subcomponentred ( modules= moduley.class)

and let's have 4 activities a, b, c , d

how make androidinjector works can have

  • a,b have own subcomponents subcomponents of subcomponentblue and

  • c,d have own subcomponents subcomponents of subcomponentred

though there isn't built-in way this, can providing implementation of hasactivityinjector in application. you'll need recreate of niceties dagger.android provides you, that's not bad.

refer default implementation daggerapplication, dispatchingandroidinjector, in maybeinject method consults multibinding-built map<class, androidinjector.factory> produce androidinjector , call inject. if you're following dagger.android users guide, every androidinjector happen subcomponent , every androidinjector.factory happen subcomponent builder installed using @contributesandroidinjector, aren't requirements.

instead, application contain code looks kind of this:

public class yourapplication extends application implements hasactivityinjector {   // let's assume application component calls inject(this) these   // @inject fields populated, , you've instantiated subcomponents   // long-lived. of course, can inject subcomponent.builder   // interfaces instead, if want new subcomponent per activity.   @inject subcomponentblue subcomponentblue;   @inject subcomponentred subcomponentred;    @override public androidinjector<activity> activityinjector() {     // if can use java 8 syntax android, lambda work     // nicely here, in "return activity -> { ... };".     return new androidinjector<activity>() {       @override public void inject(activity activity) {         if (activity instanceof activitya) {           // here's magic: know activity activitya,           // , subcomponentblue has activitya injector,           // perform cast , use builder create           // injector inject with.           activitya aactivity = (activitya) activity;           subcomponentblue.ainjectorbuilder().create(aactivity).inject(aactivity);         } elseif (activity instanceof activityb) { // ...         } elseif (activity instanceof activityc) {           activityc cactivity = (activityc) activity;           subcomponentred.cinjectorbuilder().create(cactivity).inject(cactivity);         } elseif (activity instanceof activityd) { // ...         } else {           exception atantrum = new illegalargumentexception("injector not found");           throw atantrum;         }       }     };   } } 

the above effective, pretty verbose. alternative, rather using @contributesandroidinjector, can manually bind activities multibinding map accessing them through subcomponent instead:

@module public class yourapplicationmodule {   @provides @intomap @activitykey(activitya.class)   static androidinjector.factory<activity> provideainjector(       subcomponentblue subcomponentblue) {     // of course, can inject subcomponentblue.builder ,     // create new 1 each time.     return subcomponentblue.ainjectorbuilder();   }    // same b, c, , d. } 

requisite note: in cases you're trying access injectors a, b, c, , d, outside of parent subcomponents "red" , "blue". implies you'll need make injectors (subcomponent builders) accessible part of public apis of "red" , "blue", , figure out subcomponents' lifecycles like. long-lived? per-activity? either way, isn't lifecycle other developers might expect, prepared provide ample documentation.


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -