我有一个继承的代码项目,我正在更新到Dagger的最新版本(2.25.2)
我想从使用不推荐的@Subcomponent.Builder切换到更新的@Subcomponent.Factory。我的一个症结是如何处理将变量绑定到我的视图模型。当前的模式与博客文章中描述的相同:https://medium.com/@minakamel/how-to-inject-bundle-arguments-to-viewmodel-607429829cf0
基本上使用seedInstance获取实例,然后使用BindsInstance
@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绑定实例数据的示例。
发布于 2019-11-15 08:14:04
要使用工厂绑定实例,需要将其作为参数传递给工厂方法-
@Subcomponent.Factory
interface Builder: AndroidInjector.Factory<ExampleActivity>() {
fun create(@BindsInstance currency : String) : YourSubcomponentType
}创建方法需要返回SubComponent类型。
https://stackoverflow.com/questions/58824830
复制相似问题