简体   繁体   English

如何在Dagger2中提供视图?

[英]How to Provide a View in Dagger2?

Below is the Activity, it is still using an old DI model, and i'm trying to migrate to Dagger2 (with dagger-android). 下面是活动,它仍在使用旧的DI模型,并且我正在尝试迁移到Dagger2(使用dagger-android)。 I want to inject the Presenter, and as you can see, one dependency is the 'NewProjectDisplayer' which is a custom view in the activity. 我想注入Presenter,如您所见,其中一个依赖项是“ NewProjectDisplayer”,它是活动中的自定义视图。

在此处输入图片说明

This is the 'NewProjectDisplayer' which I want to provide to the Presenter 这是我要提供给演示者的“ NewProjectDisplayer”

在此处输入图片说明

I have a Module (AppModule) which provides the global dependencies like ProjectService, LoginService etc.. 我有一个模块(AppModule),它提供了诸如ProjectService,LoginService等的全局依赖项。

Below is the NewProjectModule which I am having trouble with. 下面是我遇到麻烦的NewProjectModule。 One of the dependencies which is tied to the activity is the 'NewProjectDisplayer'. 与活动相关的依赖项之一是“ NewProjectDisplayer”。 And I have no idea how I can provide it.. 而且我不知道该如何提供..

@Module
public class NewProjectActivityModule {

    @Provides
    NewProjectDisplayer provideNewProjectDisplayer(View view) {
        // ??? return view.findViewById(R.id.create_project_view);
    }

    @Provides
    NewProjectNavigator provideNewProjectNavigator(BaseActivity baseActivity) {
        return new AndroidNewProjectNavigator(baseActivity);
    }

    @Provides
    NewProjectPresenter provideNewProjectPresenter(NewProjectDisplayer displayer, //
                                                   ProjectService projectService,
                                                   LoginService loginService,
                                                   UserService userService,
                                                   NewProjectNavigator navigator, //
                                                   PermissionHandler permissionHandler,//
                                                   CropImageHandler cropImageHandler,//
                                                   RxSchedulers schedulers) {
        return new NewProjectPresenter(displayer, projectService, loginService,
                userService, navigator, permissionHandler, cropImageHandler, schedulers);
    }

}

How can I provide this 'NewProjectDisplayer' so that the Presenter can be created and injected? 如何提供此“ NewProjectDisplayer”,以便可以创建和插入Presenter? I am using dagger-android. 我正在使用Dagger-android。

You can't provide or do a findViewById on an activity module that is compatible with dagger-android , one reason being that the module is created before the view is actually on the hierarchy. 您无法在与dagger-android兼容的活动模块上提供或执行findViewById ,原因之一是该模块是在视图实际位于层次结构上之前创建的。 Also, doing this is a not a good practice, but if you're still convinced you should do this, you could try creating a sub component that will have a module that provides the view dependency and it has to be created after setContentView , as suggested by @elmorabea. 另外,这样做不是一个好习惯,但是如果您仍然确信应该这样做,则可以尝试创建一个子组件,该子组件将具有提供视图依赖项的模块,并且必须在setContentView之后创建它,例如@elmorabea建议。

Just solving your problem, not sure if you should provide views with Dagger though. 只是解决您的问题,但不确定是否应使用Dagger提供视图。

Change your module to be something like that. 将模块更改为类似的内容。

@Module
public class NewProjectActivityModule {

private View view;

public NewProjectActivityModule (View view) {
    this.view = view;
}

@Provides
NewProjectDisplayer provideNewProjectDisplayer(View view) {
    return view;
}

}

However, where ever you are creating your Dagger component you need to do something like that. 但是,无论在哪里创建Dagger组件,都需要执行类似的操作。

YourDaggerComponent.builder().newProjectActivityModule(new NewProjectActivityModule (yourViewInstance)).build();

Otherwise it will crash at run-time. 否则它将在运行时崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM