Subcomponent.Builder -> 使用seedInstance绑定实例数据时的Factory
Subcomponent.Builder -> Factory when using seedInstance to bind instance data
我有一个继承的代码项目,我正在努力更新到最新版本的 Dagger (2.25.2)
我想从使用已弃用的 @Subcomponent.Builder
切换到较新的 @Subcomponent.Factory
。我的一个症结是如何处理实例变量到我的视图模型的绑定。当前模式与此博客 post 中描述的相同: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())
}
}
我找不到的是很多覆盖 @Subcomponent.Factory
的 create
方法以绑定实例数据的示例。
要使用 factory 绑定实例,您需要将其作为 argument 传递给工厂方法 -
@Subcomponent.Factory
interface Builder: AndroidInjector.Factory<ExampleActivity>() {
fun create(@BindsInstance currency : String) : YourSubcomponentType
}
并且创建方法需要return您的子组件类型。
我有一个继承的代码项目,我正在努力更新到最新版本的 Dagger (2.25.2)
我想从使用已弃用的 @Subcomponent.Builder
切换到较新的 @Subcomponent.Factory
。我的一个症结是如何处理实例变量到我的视图模型的绑定。当前模式与此博客 post 中描述的相同: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())
}
}
我找不到的是很多覆盖 @Subcomponent.Factory
的 create
方法以绑定实例数据的示例。
要使用 factory 绑定实例,您需要将其作为 argument 传递给工厂方法 -
@Subcomponent.Factory
interface Builder: AndroidInjector.Factory<ExampleActivity>() {
fun create(@BindsInstance currency : String) : YourSubcomponentType
}
并且创建方法需要return您的子组件类型。