【问题标题】:What is difference between ActivityRetainedComponent @ActivityRetainedScope and ActivityComponent @ActivityScoped in dagger hilt androiddagger hilt android中的ActivityRetainedComponent @ActivityRetainedScope和ActivityComponent @ActivityScoped有什么区别
【发布时间】:2020-10-12 03:50:41
【问题描述】:

dagger hilt android中的ActivityRetainedComponent @ActivityRetainedScope和ActivityComponent @ActivityScoped有什么区别

【问题讨论】:

    标签: android dependency-injection dagger dagger-hilt


    【解决方案1】:

    基于documentsActivityRetainedComponent 存在于配置更改中,因此它是在第一个 onCreate 和最后一个 onDestroy 时创建的,当您在 ActivityRetainedComponent 中使用 @ActivityRetainedScope 标记您的依赖项时,它保证您的对象将是一个单例并且在配置更改后仍然存在。但是ActivityComponentonCreate 创建并在onDestroy 销毁。当您在 ActivityComponent 中使用 @ActivityScope 标记您的依赖项时,它保证您的对象将是单例,但 ActivityComponent 将在配置更改中被销毁。

    【讨论】:

    • 第一个 onCreate 和最后一个 onDestroy 是什么?应用程序?有问题的活动?应用中有任何活动吗?
    • 使用 hilt 进行依赖注入的活动的第一个 onCreate 和最后一个 onDestroy。
    【解决方案2】:

    @ActivityRetainedScope 将防止配置更改,例如屏幕方向或语言更改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-09
      • 2011-05-05
      • 2015-11-11
      相关资源
      最近更新 更多