问题
I have a class StartRouter
. I want to provide different instances of this class to each activity, and keep it alive as long as activity is alive (for that I made @ActivityScope
).
Problem is that it does not do that in my code. It seems as dagger provides one instance per annotation, in my case, once for the @ActivityScope
. This scope is a custom dagger scope.
Here are my two modules (One for each activity), first module:
@Module
public class SplashModule {
@Provides
SplashVMFactory splashVMFactory(StartRouter startRouter){
return new SplashVMFactory(startRouter);
@Provides
StartRouter startRouter(){
return new StartRouter();
}
}
And second module:
@Module
public class QuestionModule {
@Provides
QuestionVMFactory questionVMFactory(StartRouter startRouter){
return new QuestionVMFactory(startRouter);
}
@Provides
StartRouter startRouter(){
return new StartRouter();
}
}
And I have this:
@Module
public abstract class BinderModule {
@ActivityScope
@ContributesAndroidInjector(modules = SplashModule.class)
abstract SplashActivity bindSplashActivity();
@ActivityScope
@ContributesAndroidInjector(modules = QuestionModule.class)
abstract QuestionActivity bindQuestionActivity();
}
And my application component:
@ApplicationScope
@Component(modules = {AndroidInjectionModule.class,
BinderModule.class,
SplashModule.class,
QuestionModule.class})
public interface AppComponent {
@Component.Builder
interface Builder {
@BindsInstance
Builder application(App app);
AppComponent build();
}
void inject(App app);
}
When I run this I get this error:
error: [Dagger/DuplicateBindings] com.example.StartRouter is bound multiple times:
@Provides com.example.StartRouter com.example.QuestionModule.startRouter()
@Provides com.example.StartRouter com.example.SplashModule.startRouter()
com.example.StartRouter is injected at
com.example.LoginModule.loginVMFactory(…, startRouter)
com.example.LoginVMFactory is injected at
com.example.LoginActivity.factory
com.example.LoginActivity is injected at
dagger.android.AndroidInjector.inject(T) [com.example.AppComponent ? com.example.BinderModule_BindLoginActivity.LoginActivitySubcomponent]
It is also requested at:
com.example.QuestionModule.questionVMFactory(…, startRouter)
The following other entry points also depend on it:
dagger.android.AndroidInjector.inject(T) [com.example.AppComponent ? com.example.BinderModule_BindQuestionActivity.QuestionActivitySubcomponent]
(Also have StartRouter provided in other modules than the two examples as you can see from my error).
I thought, if I have understood right, that from the usage of @ActivityScope
in BinderModule
with @ContributesAndroidInjector
would create different Components for each activity that is alive as long as the activity itself, but I seem to have understood wrong?
If I differentiate between the different StartRouter
in my @module
s by using @named
and having 1 module to provide without @named
then that will solve the problem by providing different StartRouter
instances? But that does not help me with my problem. My problem is, in short, I have trouble providing @ActivityScoped
StartRouter
instances (1 for each activity) that is alive as loong as the activity itself. Any suggestions on how I could do that?
回答1:
Your setup is fine but for the fact that you also add both modules (both binding StartRouter
) to AppComponent
, thus the error that you bind it multiple times.
@Component(modules = {AndroidInjectionModule.class,
BinderModule.class,
SplashModule.class, // <== 1.
QuestionModule.class}) // <== 2.
public interface AppComponent { .. }
Since you want one instance per Activity there is no need to add the modules to AppComponent
. You should be able to remove those 2 modules from it.
来源:https://stackoverflow.com/questions/56761359/error-dagger-duplicatebindings-com-example-startrouter-is-bound-multiple-time