question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow Fragments to depend on Activity Subcomponents when using ContributesAndroidInjector

See original GitHub issue

Consider 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:closed
  • Created 6 years ago
  • Comments:10

github_iconTop GitHub Comments

57reactions
PhilGlasscommented, Jul 3, 2017
@Module abstract class FragmentsModule {
    @ContributesAndroidInjector
    abstract FragmentA contributeFragmentA();

    @ContributesAndroidInjector(modules = ModuleB.class)
    abstract FragmentB contributeFragmentB();
}
@Module abstract class ActivityModule {
    @ContributesAndroidInjector(modules = ModuleA.class, FragmentsModule.class)
    abstract ActivityA contributeActivityA();
}

If you structure your modules like this (with ActivityModule installed in your ApplicationComponent), the generated fragment injectors will be subcomponents of your activity component & will have access to bindings declared in ModuleA.

5reactions
josh-burtoncommented, Apr 24, 2018

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).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found