android - Dagger 2 Constructor Injection and more -


i have questions regarding dagger2 find hard understand. code:

@module public class appmodule {      private mainapplication applicationcontext;      @inject     public appmodule(mainapplication context){         this.applicationcontext = context;     }      @provides     @singleton     stuffa providestuffa(){         return new stuffa();     }      @provides     @singleton     stuffb providestuffb(stuffa){          return new stuffb(stuffa);     } } 

and in application class:

appcomponent = daggerappcomponent.builder()                 .appmodule(new appmodule(this))                 .build(); 

my first question is:

  • if want split module 2, providestuffa in modulea , providestuffb in moduleb, how accomplish that, considering b depends on a.

second question:

  • i have object using in application class, , application class call injections activities. how access object in application class=? should call inject on applicationclass after dagger build stuff above?

final question

  • imagining have 2 modules different dependencies need use in same activity. proper way of having 1 component , different modules inject in same activity.

ps. i'm sorry if basic stuff, find documentation poor , lack of examples well. in advanced

answer 1

you split them in 2 separate modules. dagger build graph , when @ point providestuffb() called inject stuffa. btw think there small syntax error in provide method , should (stuffa parameter missing):

@provides @singleton stuffb providestuffb(stuffa stuffa){      return new stuffb(stuffa); } 

answer 2

yes, usual way. after create graph (in oncreate()) inject this (the application). please note there subtle caveat when in comes unit tests approach - application object created before setup() method called unable set/swap test component (and end default component). solution use lazy initialization of graph, i.e. when first activity (or service, or broadcastreceiver, etc.) calls getapplication().getinjector(this) have create graph @ point.

answer 3

you have 1 top component contains modules , subcomponents. (have be) able inject activities. de facto top component used generate graph. here example real app:

@component(modules = {izgubenodaggermodule.class,                         acradaggermodule.class,                         appinfodaggermodule.class,                         sessiondaggermodule.class,                         componentmanagerdaggermodule.class,                         networkinfoproviderdaggermodule.class,                         deviceinfoproviderdaggermodule.class,                         khrestexchangemanagerdaggermodule.class                      }) @singleton public interface izgubenodaggercomponent extends gglibdaggercomponent {     void inject(izgubenoapplication app);     void inject(act_main act);     void inject(act_contactdetails act);     void inject(act_mypets act);     void inject(act_petedit act);     void inject(act_petmanage act);     void inject(act_petpics act);     //...     gglibdaggercomponent newgglibdaggercomponent(httpsdaggermodule httpsmod,                                              khrestdaggermodule khrestmod,                                              appinfodaggermodule appinfomod,                                              ggserverdaggermodule ggservmod,                                              ggclientdaggermodule ggclientmod);     //... } 

Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -