我有一个pojo,装饰着Dagger 2的@Singleton注释
@Singleton
public class CommonDataSingleton {
private String authToken;
private boolean isAuthenticated;
}
我必须在活动中将其注入单身人士。
我创建了一个模块来告诉我应该如何创建CommonDataSingleton的对象。
@Module
public class SingletonModule {
@Provides
CommonDataSingleton getCommonDataSingleton() {
return new CommonDataSingleton();
}
}
描述应该注入对象的位置的组件
@Component(modules = {SingletonModule.class})
public interface SingletonComponent {
void inject(LoginActivity loginActivity);
void inject(LoginPresenter loginPresenter);
}
除此之外,我还有另一个用于注入完全不同对象的组件。
@Component(modules = {PresenterModule.class})
public interface DiComponent {
//to update the fields in the activities
void inject(LoginActivity loginActivity);
void inject(HomeActivity homeActivity);
}
但是我得到了这个奇怪的错误说明
DiComponent (unscoped) may not reference scoped bindings:
@Singleton test.in.singleton.CommonDataSingleton
答案 0 :(得分:0)
我会为你提供一些草图,没有经过测试。让我知道是否必须在这里进行一些编辑,但概念如下:
public class CommonDataSingleton {
private String authToken;
private boolean isAuthenticated;
}
@Module
public class SingletonModule {
@Singleton
@Provides
CommonDataSingleton getCommonDataSingleton() {
return new CommonDataSingleton();
}
}
@Singleton
@Component(modules = {SingletonModule.class})
public interface SingletonComponent {
void inject(LoginActivity loginActivity);
void inject(LoginPresenter loginPresenter);
CommonDataSingleton providesCommonDataSingleton();
}
@YourCustomScopeHere
@Component(modules = {PresenterModule.class}, dependencies = {SingletonComponent.class})
public interface DiComponent {
//to update the fields in the activities
void inject(LoginActivity loginActivity);
void inject(HomeActivity homeActivity);
}