What is actual usage of “HasFragmentInjector” in Dagger 2

后端 未结 2 454
囚心锁ツ
囚心锁ツ 2021-02-02 14:37

I have implemented dagger2 v2.2 previously but as now they have added dagger.android part also. so I am creating sample project with that.

I am aware about old methodolo

2条回答
  •  北恋
    北恋 (楼主)
    2021-02-02 15:10

    Official Documentation explains this topic quite well in my oppinion.

    Anyway the main benefit is that instead of something like this

    ((SomeApplicationBaseType) getContext().getApplicationContext())
        .getApplicationComponent()
        .newActivityComponentBuilder()
        .activity(this)
        .build()
        .inject(this);
    

    You can simply write this, which makes life easier for everyone.

    AndroidInjection.inject(this);
    
    1. Less boilerplate, easier maintainability.

    2. The previous approach is kind of breaking the basic concept of dependency injection, a class should not know any details about the way dependencies are being injected.

提交回复
热议问题