android - Dagger 2 : Unable to inject singleton in other scope

标签 android dagger-2

我有 Singleton 作用域模块,它提供一些标准的单例:应用程序、数据库服务等。 但是对于 Activity,我有单独的模块应该为他的 Activity 创建 Presenter,我需要将 Application 上下文传递给它。但是在尝试编译项目时出现以下错误:

Error:(13, 1) error: xxx.SplashComponent scoped with @xxx.ViewScope may not reference bindings with different scopes:
@Provides @Singleton xxx.ApplicationModule.provideAppContext()

这是我的应用程序模块的 fragment :

@Singleton
@Module
public class ApplicationModule {

    private Application app;

    public ApplicationModule(Application app) {
        this.app = app;
    }

    @Provides
    @Singleton
    @Named("ui")
    Scheduler provideUIScheduler() {
        return AndroidSchedulers.mainThread();
    }

    @Provides
    @Singleton
    @Named("io")
    Scheduler provideIOScheduler() {
        return Schedulers.io();
    }

    @Provides
    @Singleton
    Application provideApplication() {
        return app;
    }

    @Provides
    @Singleton
    Context provideAppContext() {
        return app;
    }
}

这里是 Activity 模块和组件:

@Module
public class SplashModule {
    private final FragmentManager fragmentManager;

    public SplashModule(FragmentManager fragmentManager) {

        this.fragmentManager = fragmentManager;
    }

    @Provides
    @ViewScope
    Presenter getPresenter(Context context) {
        return new SplashPresenter(context, fragmentManager);
    }
}

组件:

@ViewScope
@Component(modules = {SplashModule.class, ApplicationModule.class})
public interface SplashComponent {
    void inject(SplashActivity activity);
}

我做错了什么?

最佳答案

What am I doing wrong?

这个:

@ViewScope
@Component(modules = {SplashModule.class /*View scoped*/,
    ApplicationModule.class/*Singleton scoped*/})

您只能在您的组件中包含未作用域或作用域与相同作用域的模块。您将需要使用多个组件。

要包含应用程序的依赖项,您需要将它们放在不同的组件中,例如应用组件。如果您这样做,您有 2 个选择:将 SplashComponent 声明为 ApplicationComponentSubComponent 或将 ApplicationComponent 添加为对你的组件的依赖。如果将其添加为依赖项,请务必在 ApplicationComponent 中也提供方法,以便它可以访问依赖项。

例如如果您要使用组件依赖项:

@Component(modules = {ApplicationModule.class})
public interface ApplicationComponent {

    void inject(MyApplication app);

    // todo: also add getters for your other dependencies you need further down the graph
    Application getApplication();

}

@Component(modules = {SplashModule.class}, dependencies={ApplicationComponent.class})
public interface SplashComponent {
    // as before
}

关于android - Dagger 2 : Unable to inject singleton in other scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35972270/

相关文章:

android - 从谷歌示例复制粘贴代码后 list 中的错误

c# - 使用 Visual Studio 2015 社区开发安全的 Android 和 iOS 应用程序,无需像 Xamarin 这样的第 3 方许可

java - Android - 使用InputStream获取数据不返回任何内容/空

android - 从 Dagger 2 获取空实例

Android Dagger 2 和 MVP 在注入(inject)的对象中注入(inject)

java - Dagger 2 构建在单个 Inject 注释上失败

javascript - 网页如何判断它是否在没有引荐来源网址的 webapp View 中打开?

java - AppCompat 相当于 "Theme.Wallpaper.NoTitleBar.Fullscreen"

java - 无法在 Java Android 中使用 dagger2 在 fragment 中注入(inject)依赖项

android - 如何从不同的 Dagger2 组件注入(inject)同一个类?