【问题标题】:Why do we need @SubComponent in Dagger 2?为什么我们在 Dagger 2 中需要 @SubComponent?
【发布时间】:2018-06-03 11:42:35
【问题描述】:

@Subcomponent 在我们的应用程序中有什么用?

  @Subcomponent
    interface PostActivitySubComponent : AndroidInjector<PostsActivity> {

        @Subcomponent.Builder
        abstract class Builder : AndroidInjector.Builder<PostsActivity>()
    }

无论有没有这个组件,应用程序都可以正常工作。这样做的目的是什么?

【问题讨论】:

  • 子组件是一种允许范围继承的机制。

标签: java android dependency-injection dagger-2 dagger


【解决方案1】:

@SubComponent 对于您为特定屏幕提供依赖项的活动/片段的大型应用程序非常有帮助。例如,您有AppComponentSubComponent 命名为ProfileDetailedComponent,您无法控制内存,但SubComponent 可以帮助您使用@Scope 控制内存。对于每个屏幕,您将创建SubComponentSubComponent 注解都提供@CustomScope,当用户离开当前屏幕时,您可以在onDestroy() 方法中清除当前组件并释放内存。总之@SubComponent需要控制你的应用内存。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    • 2014-06-18
    • 2017-02-26
    • 2011-04-03
    • 2017-07-27
    • 2020-09-21
    • 2020-03-09
    相关资源
    最近更新 更多