-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AndroidInjection doesn't allow to inject a super class #653
Comments
@LuigiPapino Add your FragmentModule to parent component |
@UsherBaby thanks for the advice. The project is setup correctly, with the module added to the parent component. I didn't include in the example for shortness. In other words, the problem is that AndroidInjector.inject looks for the class name at runtime, so it's not able to inject a super class. Here the relevant piece of code:
However I'll prepare a project as reference to explain better the scenario. |
Here the project that replicate the scenario: https://github.com/LuigiPapino/DaggerAndroidInjectorSuperClass/tree/master A Launching the MainActivity will raise this exception: |
You need to have the |
This has been the first solution came in my mind, and I received a build error that I don't remember now. I tried again now, and works well. Don't know what mess I did. |
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? |
Could you upload the solution to your demo app? thanks! :) |
I had a same issue, too. To fix this issue, every subclass has to be added in the module explicitly even if subclass doesn't have any @Inject. For example, if there are BaseFragment and MainFragment.. @Module
public abstract class FragmentModule {
@ContributesAndroidInjector
abstract BaseFragment contributeBaseFragmentInjector();
@ContributesAndroidInjector
abstract MainFragment contributeMainFragmentInjector();
} And if you wanna fix @LuigiPapino's code, change ActivityModule.java:L19, BaseActivity.class -> MainActivity.class |
Is it possible to have the members injected for the sub classes too without specifying the sub-class name? This would be really nice, where you could use the injection interfaces among different libraries. |
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?! |
I 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:
``
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.
The text was updated successfully, but these errors were encountered: