我有一个继承的代码项目,我正在将其更新为最新版本的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
来绑定实例数据。
要使用factory