java - COnstructor Injection with roboguice enters inifinite loop -


hi come background c#/.net , have learned work bit of android.now wana start build small app fun , tought learn ioc framework.after bit of googeling found roboguice.but can not figure out how integrate it.on .

net have worked ninject , unity , , looking create similar form of constructor injection got frameworks.

here have far , think have figured out:

this class represent app bootstrapper , here register dependency config class:

public class iocapplication extends application{  @override public void oncreate(){     super.oncreate();     roboguice.setbaseapplicationinjector(this, roboguice.default_stage, roboguice.newdefaultrobomodule(this), new iocmodule()); } 

}

this dependency config class:

 public class iocmodule implements module{          @override         public void configure(com.google.inject.binder binder) {             binder.bind(itest.class).to(test.class);         }     } 

in androidmanifest have added this:

<application ... android:name="com.example.project2.iocapplication"> 

this part not realy understad why had add thinking it's tell android iocapplication should isntantiated first.

this clas mainactivily class , have added constructor it:

public itest test; public mainactivity(itest test){     this.test = test; } 

when try runthis on android device kind of looks app entering infinite loop , not think itest get's instantiated.

what doing wrong?

one thing know android you not instantiate own activities, system does.

because of this, cannot use constructor injection activities. however, can use attribute injection, cleaner imo.

extending roboactivity class easiest way use injection activity. roboguice provides similar classes other android components (robofragment, roboservice, etc.)

public class myactivity extends roboactivity {      @inject     itest test;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         // can use test instance     }  } 

all attribute @inject instantiated after super.oncreate(savedinstancestate); called.


with pojo (plain old java object), have more options:

attribute injection

public class test {     @inject     private service1 service1;     @inject     private service2 service2;      public test() {     } } 

constructor injection

public class test {      private service1 service1;     private service2 service2;      @inject     public test(service1 service1, service2 service2) {         this.service1 = service1;         this.service2 = service2;     } } 

note constructor must have @inject annotation if has arguments.


you need line <application ... android:name="com.example.project2.iocapplication"> tell system using extended application class. without it, android use base class.

i encourage read official documentation more information.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -