Recently I downloaded Roboguice and gave it a try. In general I like it and I think it could ease some aspects in Android development process, but I encountered a situation which didn’t find a solution yet: I want to inject a class, but that class has one, ore more constructors with several parameters.
In a such case, how would I specify which constructor to choose for instantiation, and pass the values to constructor?
For example I have the the class TestRobo with 2 constructors, and I want to instantiate the object from the second constructor, passing the firstName, lastName as parameters:
public class TestRobo implements ITestRobo {
public TestRobo(String fullName) {
//....
}
public TestRobo(String firstName, String lastName) {
//...
}
}
Right now, if I inject it like this:
@Inject
private ITestRobo testRobo;
It trows an exception that it couldn’t find a suitable constructor.
You need to bind ITestRobo to its TestRobo implementation. You can either do this by adding the @ProvidedBy(TestRobo.class) annotation to ITestRobo, or you can add a module and bind(ITestRobo.class).to(TestRobo.class) in your configure() method.