15

I have an abstract base class in my project

public abstract class BaseActivity<T extends BasePresenter<? extends IBaseView>> implements IBaseView{

Into which I try to inject a generic class like this:

@Inject protected T mPresenter; 

Is there any way to make dagger work with such a generic injection? Dagger generates code like:

public final class BaseActivity$$InjectAdapter extends Binding<BaseActivity>
implements MembersInjector<BaseActivity> {
    private Binding<T> mPresenter;
}

And then fails because "T cannot be resolved to a type". Is there any way to make it generate a

Binding<SomethingExtendingBasePresenter> mPresenter

in such case?

Malthan
  • 7,005
  • 3
  • 20
  • 22
  • Your types are very odd. Why is an `Activity` extending an interface for a `View`? – Christopher Perry Jan 15 '14 at 07:23
  • 2
    The Activity is implementing a BaseView interface, it's View from Model-View-Presenter pattern, not View as in android.view.View – Malthan Mar 18 '14 at 12:59
  • In that case, since your Activity is a presenter it probably shouldn't also be a View. – Christopher Perry Mar 18 '14 at 18:09
  • 1
    The Activity isn't a Presenter - it just implmenets the View interface. So: Presenter (represented by a fooPresenter class) works on the IFooView interface, that's implemented by a fooActivity class. I want to inject a class that's a subclass of the BasePresenter into a class that's a subclass oh BaseActivity, based on the generic type T. The code above works in RoboGuice, but I can't get it to work in Dagger. – Malthan Mar 21 '14 at 13:23
  • Can you post your module? – Christopher Perry Mar 22 '14 at 00:10
  • Why is the `View` interface not implemented by the `android.view.View`? – Nelson Osacky Aug 03 '14 at 05:57
  • @NelsonOsacky it could be - the whole point of using an interface is that it can be implemented by whatever best suits the situation, since the logic stays untouched in the presenter. Currently I mostly use fragments, but sometimes it's an Activity or a View. – Malthan Aug 17 '14 at 22:31

1 Answers1

1

The only way I know of is to create a new class that extends your generic. One for each type you are interested in.

public class Foo extends BaseActivity<SomethingExtendingBasePresenter> {
}

@Inject
Foo mFoo;