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?
Dagger is a fully static, compile-time dependency injection framework for Java, Kotlin, and Android. It is an adaptation of an earlier version created by Square and now maintained by Google.
Dagger automatically generates code that mimics the code you would otherwise have hand-written. Because the code is generated at compile time, it's traceable and more performant than other reflection-based solutions such as Guice. Note: Use Hilt for dependency injection on Android.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With