Question

HI I come from a background C#/.NET and have learned to work with a bit of Android.Now I am wana start to build a small app for fun and I tought to learn an IoC framework.After a bit of googeling I found roboguice.But I can not figure out how to integrate it.On .

NET I have worked with Ninject and Unity , and am looking to create a similar form of constructor injection that I got from those frameworks.

Here is what I have so far and what I think I have figured out:

This class represent the app bootstrapper and here is where I will register my 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 is the 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 my AndroidManifest I have added this:

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

This part I do not realy understad why I had to add but I am thinking it's something to tell Android that IOCApplication should be isntantiated first.

This is the clas my MainActivily class and I have added a constructor for it:

public ITest test;
public MainActivity(ITest test){
    this.test = test;
}

When I try to runthis on my android device it kind of looks like the app is entering an infinite loop and I do not think ITest get's instantiated.

What am I doing wrong?

Was it helpful?

Solution

One thing to know with Android is that you do not instantiate your own activities, it is the system which does.

Because of this, you cannot use constructor injection with activities. However, you can use attribute injection, which is cleaner IMO.

Extending the RoboActivity class is the easiest way to use injection with Activity. RoboGuice provides similar classes for other Android components (RoboFragment, RoboService, etc.)

public class MyActivity extends RoboActivity {

    @Inject
    ITest test;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // You can now use your test instance
    }

}

All attribute with the @Inject will be instantiated after super.onCreate(savedInstanceState); is called.


With POJO (Plain Old Java Object), you 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 that your constructor must have the @Inject annotation if it has arguments.


You need this line <application ... android:name="com.example.project2.IOCApplication"> to tell the system that you are using an extended Application class. Without it, Android will use the base class.

I encourage you to read the official documentation for more information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top