2

Is there a way to inject an Android Activity subclass into a POJO using Roboguice?

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
Julian A.
  • 10,928
  • 16
  • 67
  • 107

1 Answers1

6

Firstly this is my first ever post on here, so I am being brave and hopefully I can answer your question, and hopefully not go into negative figures as soon as I start.

Bare in mind I have only just discovered Roboguice in the last couple of days so hopefully I can help out here. Also my first stab at Java from using .Net so much so apologies if i have the incorrect syntax for the usual Java style.

I guess it depends on which Activity you want, the activity that creates the instance of the Pojo or another activity.

I will try to put an example here for both on what I set up and tried out.

public interface IMySecondActivity {}
public class MySecondActivity extends RoboActivity implements IMySecondActivity {}

public interface ITestPojo {}
public class TestPojo implements ITestPojo 
{
  @Inject public TestPojo(IMySecondActivity mySecondActivity, Activity activity)
  {
    //So at this point here mySecondActivity is an instance of IMySecondActivity    
    //set up by roboguice
    //and activity is the activity which created this TestPojo
  }
}

public class TestAppModule extends AbstractAndroidModule 
{
  @Override protected void configure()
  {
    bind(ITestPojo.class).to(TestPojo.class);
    bind(IMySecondActivity.class).to(MySecondActivity.class);
  }
}

public class MyActivity extends RoboActivity implements IMyActivity
{
  @Inject ITestPojo testPojo;

  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }
}

So I have configured the Module for RoboGuice to know how to bind ITestPojo and IMySecondActivity.

Lets assume we are currently on the MyActivity activity, when this starts up, an instance of TestPojo is injected in. During this injection its constructor is called and the parameters to TestPojo constructor are resolved, IMySecondActivity via the binding and Activity from the containing MyActivity.

However, I guess care will have to be taken with this situation as testPojo will still be null inside MyActivity, as TestPojo is still in construction.

There is also another way we could get the Activity and that is to use the Provider

Provider<Activity> activityProvider;
public TestPojo(Provider<Activity> activity)
{
  activityProvider = activity;
}

public void Test()
{
  //This should retrieve your activity.
  Activity activity = activityProvider.get();
}

in the TestPojo class so that the provider can be asked at a later time to retrieve the Activity at the point where its used.

If you know the activity type your pojo is going to use then you can use Provider<MyActivity> instead.

I hope this helps out :-)

  • :) thanks for taking the time to respond, and in such detail too. Are you sure that the instance provided to the `TestPojo` constructor is the **same** instance of `MyActivity` that `TestPojo` is being injected into, and not a new instance of `MyActivity`? RoboGuice must not be asked to create new instances of Activity or any other Android framework-managed classes because, though it might readily do so (especially for classes with no-arg constructors), those instances are unusable because Android framework has no knowledge of their existence. Same applies to your alternative solution. – Julian A. Feb 21 '12 at 00:07
  • I am unsure, in work at mo and will have to check but I am assuming when your activity derives from RoboActivity it will do its magic to manage and inject the containing Activity to the TestPojo. I'm assuming it will be the Activity that creates the instance of the TestPojo as the parameter "Activity activity" to the TestPojo constructor was a MyActivity as I haven't told it what to create and I've more than 1 Activity in the system, roboguice didn't complain about any ambiguous Activty to resolve, so it gave me a MyActivity, :-S meh does that make sense? Sorry 2 many assumptions here. – RichardWilliams Feb 21 '12 at 08:49
  • Also I just had a thought, wouldn't it get recursively stuck if it was a different Activity, as each time it was created a new TestPojo it would have to be created, then another Activty... etc I understand it would be bad for RoboGuice to create a new instance of an Activity that is not managed by Android. Are you intending to use this so that RoboGuice needs to know if there is an Activity that already exists created by TestPojo and for RoboGuice to use this when the Android framework asks for this Activity? – RichardWilliams Feb 21 '12 at 19:06