0

I have been forcing myself to use dagger-android for my new project, in order to reduce all the boiler plate for subComponents for activities and fragments.

But struggling with getting my @UserScope deps to be injected in those activities.

Reading up dagger-android it seems to support this kind of object graph:

Application -> Activity -> Fragment -> Sub-fragments

In my case I need a UserScope to sit between Application and Activity. i.e

                      |-> Activity -> Fragment -> Sub-fragments`
Application -> User - |
                      |-> Activity -> Fragment -> Sub-fragments`

I was wondering if there is way to achieve this WITH using ContributesAndroidInjector along with a custom subcomponent.

Any advice.

Couple of similar threads:

duskandawn
  • 646
  • 1
  • 10
  • 19

1 Answers1

0

If you want to use AndroidInjection and ContributesAndroidInjector I don't think that is possible, if not in some degenerate way.

To be more precise, when you use AndroidInjection the code is getting the Application instance from your class, and then using the HasActivityInjector the App has. Therefore the activities' sub-components must be sub-components of the Component which injected the Application class at startup.

And when the Application class is created you can't really be in a scope different than an Application/singleton scope.

Probably the question is: what is in the User scope (and not in the application scope) and where does that come from?

  • If you can get that when the app starts before starting any activities, then just merge the application and the user components.

  • If you 'create' then user scope from some data you get from, say, an activity, then you'll have the activities pass some data using intents when they launch each other.

al3c
  • 1,392
  • 8
  • 15