【问题标题】:Why Hilt and Dagger need to inject Activity and Fragment?为什么 Hilt 和 Dagger 需要注入 Activity 和 Fragment?
【发布时间】:2021-03-30 14:51:58
【问题描述】:

我使用 Dagger 已经有一段时间了,现在我开始迁移到 Hilt,但是我不明白注入 Activity 和 Fragment 的好处。 在 Dagger 中,它是 AndroidInjection.inject(this) 和类似 HasActivityInjector

对不起,如果这是一个愚蠢的问题。

【问题讨论】:

    标签: android kotlin dagger-2 dagger dagger-hilt


    【解决方案1】:

    这不是好处,而是 Android 强加的约束。 Activity 和 Fragment 由 Android 实例化,而不是由开发人员或 DI 框架实例化,因此需要特殊处理。 It is the same in Dagger actually.

    在 Hilt 中,您不再需要手动编写该代码,you can just annotate the Application, Activity and Fragment using special notations,库将负责生成相应的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多