@arnaud.giuliani weird that in 2.2.0 and Kotlin 1.4.0 the last item in
parametersOf()
must be cast to
Any
and could not be nullable.
Type mismatch: inferred type is String? but Any was expected
Also could you tell me if I have updated a ViewModel instantiation correctly?
I have replaced the old code
viewModel = lifecycleScope.getViewModel<FormsViewModel>(this) {
parametersOf(
intent?.getBooleanExtra(ProcessHelper.IntentParamShouldReset, false) ?: false,
intent?.getStringExtra("processName")
)
}
to
val viewModel = scope.getViewModel<FormsViewModel>(null, emptyState(),
{ ViewModelOwner.from(viewModelStore) }, parameters = {
parametersOf(
intent?.getBooleanExtra(ProcessHelper.IntentParamShouldReset, false)
?: false,
intent?.getStringExtra("processName") as Any,
)
}
)
Old implementation looks much more cleaner than the current one and also there are many
viewModel(...)
methods, that I found confusing in terms of which I should use. If you could provide some suggestion or samples how it should be used, that will be very much appreciated.
Will describe what I want to achieve in case you might know the easier way 😄
I have an activity that initiates view model and all the fragments and child fragments inside that activity are able to access this view model if needed via Activity. And during activity lifecycle the same instance of ViewModel is delivered to fragments via `getSharedViewModel` call.
In 2.1.6 I done this via activity `lifecycle`
module definition looks like this:
scope(named<MyActivity>()) {
viewModel { (intent: Intent?) -> MyViewModel(get(), get(), intent) }
}