匕首2三部分注射的Andr​​oid [英] Dagger 2 Third part Injection Android

查看:104
本文介绍了匕首2三部分注射的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨即时试图找出如何做一个干净的第三方注入。我想正确注入奥托巴士到我的工作和活动。
四看出,您可以使用构造函数注入,但因为我没有与Android的构造,我不知道我怎么能那么注入我的公交车。

Hi im trying to figure out how to do a clean third party injection. I want to inject Otto bus properly into my services and activities. Iv seen that you can use inject on constructor, but since I dont have any constructor with Android, i wonder how I can then inject my bus.

㈣创建一个模块,它提供了总线的新实例。 IV还创造了它可以对总线对象的接口的组件。

Iv created a module which provides a new instance of the bus. Iv also created a component which has an interface for the Bus object.

但我怎么能得到这个注入,我应该在哪里开始我的图表?

But how can I get this injected and where should I initiate my graph?

由于从匕首1 objectGraph被删除,我用匕首_....组件并创建()的应用程序类,但我应该怎么把它注射到任何活动或服务?

Since the objectGraph from Dagger 1 is removed, i use the Dagger_.... component and create() in the application class, but how should I inject it into whatever activity or service?

我应该建立在每一个的onCreate组件,并从那里得到的公交车?或者是有可能像@注入1匕首?请告诉我,因为现在它似乎更块状和复杂得多做的匕首1路。

Should I create the component in every onCreate and get the bus from there? Or is it possible to @Inject like Dagger 1? Please tell me because right now it seems much more clumpy and complicated than Dagger 1 way of doing it.

@Component(modules = EventBusModule.class)
@Singleton
public interface EventBus {
    Bus bus();
}

@Module
public class EventBusModule {

   @Provides
   @Singleton
   public Bus provideBus() {
       return new Bus(ThreadEnforcer.ANY);
   }
}

所有我希望能够做的是:

All i want to be able to do is:

public class WearService extends WearableListenerService {
    private static final String TAG = WearService.class.getSimpleName();

    @Inject
    protected Bus bus;

   @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        bus.register(this);
        return START_STICKY;
    }
}

我看看下面这个例子( https://github.com/LiveTyping/u2020-mvp),并看到它的可能,但不知道事情是怎么挂在一起。

I look at this example(https://github.com/LiveTyping/u2020-mvp) and see that its possible, but not sure how things is hanging together.

推荐答案

对于我的问题有些discususins​​与NIEK后,我去了源找到我的答案。我问匕首上的2 GitHub的回购,答案可以在这里找到:

After some discususins with Niek regarding my question, I went to the source to find my answer. I asked on the github repo of dagger 2, and the answer can be found here:

https://github.com/google/dagger/issues/128# issuecomment-86702574

它非常审慎和职位不同的解决方案,以我的问题。

Its very deliberate and posts different solutions to my issue.

这篇关于匕首2三部分注射的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆