I have an inherited code project that I am working on updating to the latest version of Dagger (2.25.2)
I want to switch from using the deprecated @Subcomponent.Builder
to the newer @Subcomponent.Factory
. My one sticking point is how to handle binding for instance variables to my view model. The current pattern is the same as is described in this blog post: https://medium.com/@minakamel/how-to-inject-bundle-arguments-to-viewmodel-607429829cf0
Basically using seedInstance
to get the instance then using 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())
}
}
What I have been unable to find are very many examples of overriding the create
method for @Subcomponent.Factory
to bind the instance data.
To bind an instance using factory you need to pass it as an argument to factory method -
@Subcomponent.Factory
interface Builder: AndroidInjector.Factory<ExampleActivity>() {
fun create(@BindsInstance currency : String) : YourSubcomponentType
}
And the create method needs to return your SubComponent Type.