Dagger 2 on Android. Different ways to store and access a @Singleton Component

前端 未结 2 1113
孤街浪徒
孤街浪徒 2021-02-19 11:15

This is the Nth question about how to store @Singleton scoped Dagger 2 Components whose lifetime should equal the application\'s lifetime.

In Android apps using Dagger 2

2条回答
  •  隐瞒了意图╮
    2021-02-19 11:53

    With 1 and 2 you are using static references. This is a good thread about why to avoid them

    Why are static variables considered evil?

    So the only option left is the 3rd. That is what I am using on my projects. About if you should pass the context as argument or not, depends on the architecture of your project and how you designed the Dagger dependencies. Personally I don't have that problem because I am only injecting in Activities/Fragments. Can you give me an example where you need to pass the context to inject dependencies?

提交回复
热议问题