首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Subcomponent.Builder -> Factory绑定实例数据时

使用Subcomponent.Builder -> Factory绑定实例数据时
EN

Stack Overflow用户
提问于 2019-11-12 18:44:12
回答 1查看 176关注 0票数 0

我有一个继承的代码项目,我正在更新到Dagger的最新版本(2.25.2)

我想从使用不推荐的@Subcomponent.Builder切换到更新的@Subcomponent.Factory。我的一个症结是如何处理将变量绑定到我的视图模型。当前的模式与博客文章中描述的相同:https://medium.com/@minakamel/how-to-inject-bundle-arguments-to-viewmodel-607429829cf0

基本上使用seedInstance获取实例,然后使用BindsInstance

代码语言:javascript
复制
@Subcomponent.Builder
abstract class Builder: AndroidInjector.Builder<ExampleActivity>() {

    abstract override fun build(): ExampleComponent

    @BindsInstance
    abstract fun currency(currency: String): Builder

    override fun seedInstance(instance: ExampleActivity) {
       currency(instance.getCurrencyFromBundle())
    }
}

我无法找到的是许多重写create方法以使@Subcomponent.Factory绑定实例数据的示例。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-15 08:14:04

要使用工厂绑定实例,需要将其作为参数传递给工厂方法-

代码语言:javascript
复制
@Subcomponent.Factory
 interface Builder: AndroidInjector.Factory<ExampleActivity>() {

    fun create(@BindsInstance currency : String) : YourSubcomponentType

}

创建方法需要返回SubComponent类型。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58824830

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档