AndroidInjection doesn't allow to inject a super class
See original GitHub issueI discovered this issue playing with AndroidAnnotations but it should be the same for every scenario where a Fragment extends another Fragment and the base one use AndroindInjection.inject
.
The detailed scenario:
MyFragment use AndroidInjection to inject the presenter. Following the Subcomponent, module and Fragment:
@Subcomponent
public interface MyFragmentSubComponent extends AndroidInjector<MyFragment> {
@Subcomponent.Builder
abstract class Builder extends AndroidInjector.Builder<MyFragment> {
}
}
@Module(subcomponents = { MyFragmentSubComponent.class })
abstract class FragmentModule {
@Binds
@IntoMap
@FragmentKey(MyFragment.class)
abstract AndroidInjector.Factory<? extends Fragment> bindMyFragmentInjectorFactory(
MyFragmentComponent.Builder builder);
}
@EFragment(R.layout.screen_my_fragment)
public class RMyFragment extends BaseFragment implements RecipeBrowserPageView {
@Inject
MyPresenter presenter;
@Override
public void onAttach(Context context) {
AndroidSupportInjection.inject(this);
super.onAttach(context);
}
}
``
AndroidAnnotations will generate the class MyFragment_ extending MyFragment.
If I try to use MyFragment_ this error will be raised:
Caused by: java.lang.IllegalArgumentException: No injector factory bound for Class<com.package.MyFragment_>. Injector factories were bound for supertypes of com.package.MyFragment_: [com.package.MyFragment]. Did you mean to bind an injector factory for the subtype?
Because obviously the subcomponent is for the base MyFragment.
I’d like to try adding a class parameter to AndroidInjector.inject, and force to look for the correct injector based on the class parameter, and inject the instance parameter. It seems possible to you?
Could anyone suggest a different approach or a solution for this problem?
Thanks very much.
Issue Analytics
- State:
- Created 6 years ago
- Comments:10
Top GitHub Comments
I struggle with the same problem. I want to write the Dagger Code only once for my BaseActivity, so all other activities inherits from this one and could easily be injected. But as I get the Dagger 2 concept right that means, I have to write a SubComponent, a Module and an injector factory for all my 10 Activities. In what way should that kind of DI makes developers live easier?!
Hi @LuigiPapino you said that your demo works well. I cloned your repository and had the same issue, do you mind to check if some changes was not pushed to your demo?