Dagger不能在我的活动中注入Presenter

时间:2014-11-17 17:13:59

标签: android dagger

我使用Dagger 1将Android应用重构为MVP架构。

我的代码或多或少如下:

public interface View {
    Presenter getPresenter(); 
}

public abstract class Presenter {
    // Dagger 1 can't have an abstract class without injectable members... :(
    @Inject
    Application dont_need_this;

    protected View view;

    public void takeView(View view) { ... }
}

Presenter中的字段并不漂亮,但我们能做些什么?

接下来,我的Activity也是View

public abstract class ActivityView extends Activity implements View {
    @Inject
    protected Presenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ObjectGraph scope;

        // Looks for an "scoped" ObjectGraph hold in an retained fragment

        if (/* fragment is null */) {
            // This is not a rotation but the first time the activity is 
            // launched

            MVPApplication app = (MVPApplication) getApplication();
            ObjectGraph scope = app.createScope(this);

            // Store in the fragment
         }

         scope.inject(this);

         // setContentView, etc.

         presenter.takeView(this);
    }

}

现在一切似乎都很好......

public abstract class MVPApplication extends Application {

        private ObjectGraph objectGraph;

        @Override
        public void onCreate() {
            super.onCreate();

            // Root scope
            // createRootModules is defined in the subclass
            objectGraph = ObjectGraph.create(createRootModules()); 
            objectGraph.inject(this);
         }

         public abstract ObjectGraph createScope(View view);
 }

有了这个,我的应用程序子类必须:

  • 定义createRootModules()以实例化根范围中的每个模块
  • 检查createScope()具体ActivityView的类,主要使用HashMap<Class<? extends View>, Module>并执行...

return objectGraph.plus(new ModuleForThisActivityViewClass());

例如,我有CollectionActivityView。我的应用试图...

return objectGraph.plus(new CollectionModule());

我在这个模块中对这个特定的Presenter有绑定:

@Module(addsTo = MyAppModule.class,
    injects = {CollectionActivityView.class, CollectionPresenter.class}, complete=false)
public class CollectionModule {
    @Provides
    @Singleton
    public Presenter providePresenter(CollectionPresenter presenter) {
        return presenter;
    }
}

但是在执行&#39; plus()时会发生以下错误:

ComponentInfo{com.mycompany.myapp/com.mycompany.myapp.view.CollectionActivityView}:      
java.lang.IllegalStateException: Unable to create binding for 
    com.mycompany.myapp.mvp.presenter.Presenter

...

 Caused by: java.lang.IllegalStateException: Unable to create binding for 
    com.mycompany.myapp.mvp.presenter.Presenter
        at dagger.internal.Linker.linkRequested(Linker.java:147)
        at dagger.internal.Linker.linkAll(Linker.java:109)
        at dagger.ObjectGraph$DaggerObjectGraph.linkEverything(ObjectGraph.java:244)
        at dagger.ObjectGraph$DaggerObjectGraph.plus(ObjectGraph.java:203)    

Dagger试图通过负责提供Presenter的新模块来解决在Presenter 之前的<{1}} 加上ObjectGraph的超级类ActivityView的悬空依赖关系。

有没有办法避免这种情况?我做错了什么吗?你有什么建议吗?

1 个答案:

答案 0 :(得分:0)

再看一遍,这是我的错。

我不知道为什么要这样做(可能是为了避免将模块标记为不完整或库),但MyAppModule声明它正在注入CollectionActivityView,因此Presenter插入另一个之前,必须在该模块中解决依赖关系。从MyAppModule中的注释中删除了它,一切都按预期工作。