I've been trying to extend an approach shown in GithubBrowserSample to support multiple Dagger Components.
I currently have two components: one with a @Singleton scope that is stored for the whole life of the application, and a dependent component scoped @PerUser, that gets recreated every time a new user logs in.
In the example, DispatchingAndroidInjector is used as AndroidInjector for injecting activities. DispatchingAndroidInjector is provided by Dagger, but I do not see a way to produce such an Injector (or a Map needed for its creation) from two components automatically using Dagger annotations. Should I create two maps and a new subclass of AndroidInjector to handle them?
Also, I see that @ContributesAndroidInjector is a convenient way to create Dagger components for each Activity or Fragment. I see that I can add @Modules and @Scopes to them, but I don't understand their lifecycle. Is there a way to keep these components through the Activity configuration changes?
Google please give us an example app on how to create UserScoped dependencies (for example: dependencies that recreate when FirebaseAuth.AuthStateListener returns a new user) with AndroidInjector. Pretty please :)
Here is how I ended up doing it:
@Subcomponentsvoid inject(YourApplication app); to all of them or make all of them extend a common interface that has this methodYourApplication.onCreate() build root component and use it to inject SomeScopedSubcomponent.Builder into YourApplicationSomeScopedSubcomponent use already injected Builder to build it, and then call SomeScopedSubcomponent.inject(yourapplication). This will replace the AndroidInjector with the one that has scoped dependencies.What you have to keep in mind:
There is a compile time validation checking if an Activity requires some dependency from the scoped component. Then corresponding @ContributesAndroidInjector for this Activity should be set up in this scoped component. But it does not mean that this Activity will be started while your application is injected with this scoped component. Such cases include:
Activity that requires scope using a deep link while app is currently in another scope.Activity from recent apps, in an app that does not persist scope.Also note that difficulties of using scopes that were before dagger.android are still there:
ViewModelProvider.Factory). You do not have direct control over injection of the objects that factory creates, but they still may hold parts of your dependency tree.Here I created an example of how it could be done based on google's sample. I added an ability to log in (although it does not provide any additional functionality and stores credentials only in memory). When you log in, the scope is changed from @BeforeLoginScope to @AuthorizedScope, and is changed back when you log out.
Closing this. Looks the OP found a solution.
Most helpful comment
Google please give us an example app on how to create
UserScopeddependencies (for example: dependencies that recreate whenFirebaseAuth.AuthStateListenerreturns a new user) withAndroidInjector. Pretty please :)