Allow Fragments to depend on Activity Subcomponents when using ContributesAndroidInjector
See original GitHub issueConsider the below example:
@Module
abstract class BuildersModule {
@ActivityScope
@ContributesAndroidInjector(modules = ModuleA.class)
abstract ActivityA contributeActivity();
@ActivityScope
@ContributesAndroidInjector
abstract FragmentA contributeFragmentA();
@ActivityScope
@ContributesAndroidInjector(modules = ModuleB.class)
abstract FragmentB contributeFragmentB();
}
The two Fragments are children of ActivityA. We don’t have an easy means of sharing Activity-scoped bindings between the Activity and the Fragments because all the generated subcomponents hang off ApplicationComponent.
It would be great if we could : 1) share ActivityA’s component with FragmentA 2) Specify FragmentB’s component as a Subcomponent of ActivityA’s component.
Issue Analytics
- State:
- Created 6 years ago
- Comments:10
Top Results From Across the Web
How to use Android Injector for Activity and Fragment objects ...
Let's start to write ActivitiesModule as a subcomponent, we will use @ContributesAndroidInjector annotation type.
Read more >Dagger subcomponent with contributor - Stack Overflow
Dagger android does injection by finding the closest injector in the current scope. For Fragment, it is the containing Activity and for ...
Read more >Dagger by Tutorials, Chapter 16: Dagger & Android | Kodeco ...
When to use @Component s or @Subcomponent s to manage dependencies between ... You had to write this code in all the Activity...
Read more >Using Dagger-Android in a multi-module project
It is in this ActivityBindingModule where we create the feature1 subcomponent that have AppComponent as parent which depends on CoreComponent ...
Read more >Dagger Series: Elegantly handle your Activities ... - 舊金山周記
Dagger-Android uses this entry to build an Activity Subcomponent and ... create a simple module with @ContributesAndroidInjector annotation.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If you structure your modules like this (with
ActivityModule
installed in yourApplicationComponent
), the generated fragment injectors will be subcomponents of your activity component & will have access to bindings declared inModuleA
.Does the above approach still work? I’ve tried to implement it and dagger complains that it can’t find an android injector for the fragments.
Are there any extra steps needed to get this to work?
Edit: I’ve found the issue - my activities weren’t extending from DaggerActivity (I was calling AndroidInjection.inject manually).