匕首2-如何仅注入基础活动/片段 [英] Dagger 2 - how to inject only to base activity/fragment

查看:73
本文介绍了匕首2-如何仅注入基础活动/片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从许多此类来源研究Dagger 2: http://fernandocejas.com/2015/04/11/tasting-dagger-2-on-android/
,但我仍未找到问题的答案。

I am studying a Dagger 2 from many sources such as this one: http://fernandocejas.com/2015/04/11/tasting-dagger-2-on-android/ but I still haven't found an answer to my question.

我在一个非常复杂的应用程序上工作,该应用程序包含数十个片段和一些我想使用DI的活动(匕首2)。对于所有这些片段和活动,我都有一个BaseActivity和一个BaseFragment。但是,就我阅读和尝试而言,为了在我的MainActivity中使用@Inject,我必须在Component接口中指定它,还必须在onCreate方法中调用getApplicationComponent()。inject(this)。当我仅对BaseActivity执行此操作时,不会在MainActivity中注入@Inject带注释的字段。更糟糕的是,直到执行该代码的特定部分并抛出NPE时,我才知道这一点。

I work on quite complex application with tens of fragments and several activities in which I want to use DI (dagger 2). For all of those fragments and activities I have one BaseActivity and one BaseFragment. However, as far as I read and tried, in order to use @Inject in my let's say MainActivity, I have to specify it in Component interface and also invoke getApplicationComponent().inject(this) in onCreate method. When I do this for BaseActivity only, @Inject annotated fields in MainActivity is never injected. And what is even worse, I do not find out about that until that specific part of code is executed and NPE is thrown.

到目前为止,对我来说这是一个大问题,因为这可能会导致许多崩溃。我将需要在Component接口中指定数十个片段和活动,并且不要忘记在每个onCreate方法中调用inject。

So far it is a deal breaker for me, because this can be source of many crashes. I would need to specify tens of fragments and activities in Component interface and not forget to call inject in each onCreate method.

我很高兴听到对此的任何解决方案因为我真的很想使用DI。

I would be very glad to hear any solution to this since I would really like to use DI..

代码示例:

@Singleton
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
    void inject(BaseActivity baseActivity);
    Analytics analytics();
}

public class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getApplicationComponent().inject(this);
    }
}

public class MainActivity extends BaseActivity {
    @Inject
    Analytics analytics;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        analytics.log("event1"); // THROWS NPE!
    }
}


推荐答案

您可以通过注入super在子类中 not 注入属性(因为dagger2在编译时起作用,并且没有办法动态检查子类中带注释的属性。)

You can not inject properties in your subclass by injecting the super (since dagger2 works at compile time and there is no way to dynamically check subclasses for annotated properties.)

您可以将 analytics 移至上级,然后将其注入该级中。要在子类中注入带注释的字段,您将不得不在那儿再次调用注入。

You can move analytics up to the super, then it will be injected there. To inject annotated fields in your subclass you will have to call the injection there again.

您可以在基类中创建一个抽象方法,例如 inject(应用程序应用),您只需在其中处理注入。这样一来,您就不会'错过'。

You can make an abstract method in your baseclass e.g. inject(App app)where you just handle the injection. That way you can't 'miss' it.

如官方文档


类型的members-injection方法将接受其子类型的实例,仅参数类型及其超类型的带有Inject注释的成员将被注入;

While a members-injection method for a type will accept instances of its subtypes, only Inject-annotated members of the parameter type and its supertypes will be injected; members of subtypes will not.

这篇关于匕首2-如何仅注入基础活动/片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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