Hope to find some help here after days and days researching about this very interested subject "Inherited subcomponent multibindings
which you can find here Inherited subcomponent multibindings which is the last subject in that page.
According to the official documentation:
subComponent
can add elements tomultibound
sets or maps that are bound in its parent. When that happens, the set or map is different depending on where it is injected. When it is injected into a binding defined on thesubcomponent
, then it has the values or entries defined by the subcomponent’smultibindings
as well as those defined by the parent component’smultibindings
. When it is injected into a binding defined on the parent component, it has only the values or entries defined there.
In other words. If the parent Component
has a multibound set or map
and a child component
has binding to that multibound, then those binding will be link/added into the parent map depending where those binding are injected within the dagger scope if any.
Here is the issue.
Using dagger version 2.24
in an Android Application using Kotlin
. I have an ApplicationComponent
making use of the new @Component.Factory
approach. The ApplicationComponent has installed the AndroidSupportInjectionModule
.
I also have an ActivitySubComponent
using the new @Component.Factory
approach and this one is linked to the AppComponent using the subComponents
argument of a Module annotation.
This ActivitySubComponent provides a ViewModel
thru a binding like this
@Binds
@IntoMap
@ViewModelKey(MyViewModel::class)
fun provideMyViewModel(impl: MyViewModel): ViewModel
the @ViewModelKey
is a custom Dagger Annotation.
I also have a ViewModelFactory implemented like this.
@Singleton
class ViewModelFactory @Inject constructor(
private val viewModelsToInject: Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>
) : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T =
viewModelsToInject[modelClass]?.get() as T
}
A normal ViewModelFactory
The difference here is that I am providing this ViewModelFactory in one of the AppComponents modules. But the bind viewModels within the ActivitySubComponent are not getting added into the ViewModelFactory Map in the AppComponent.
In other words. What the documentation is describing is not happening at all.
If I move the viewModels binding into any of the AppComponent Modules, then all work.
Do you know what could be happening here.