【发布时间】:2020-10-12 03:50:41
【问题描述】:
dagger hilt android中的ActivityRetainedComponent @ActivityRetainedScope和ActivityComponent @ActivityScoped有什么区别
【问题讨论】:
标签: android dependency-injection dagger dagger-hilt
dagger hilt android中的ActivityRetainedComponent @ActivityRetainedScope和ActivityComponent @ActivityScoped有什么区别
【问题讨论】:
标签: android dependency-injection dagger dagger-hilt
基于documents。 ActivityRetainedComponent 存在于配置更改中,因此它是在第一个 onCreate 和最后一个 onDestroy 时创建的,当您在 ActivityRetainedComponent 中使用 @ActivityRetainedScope 标记您的依赖项时,它保证您的对象将是一个单例并且在配置更改后仍然存在。但是ActivityComponent 在onCreate 创建并在onDestroy 销毁。当您在 ActivityComponent 中使用 @ActivityScope 标记您的依赖项时,它保证您的对象将是单例,但 ActivityComponent 将在配置更改中被销毁。
【讨论】:
@ActivityRetainedScope 将防止配置更改,例如屏幕方向或语言更改。
【讨论】: