feat: added support to have navigator scoped ViewModel
ISSUES SOLVED: #155 #168 and #210
Navigator scoped screen model was added on the #233
AndroidX ViewModel has support to scoped ViewModel here
So, we are supporting scoped ViewModel in Voyager too. How it works?
Androidx has a function viewModel {} that use the local ViewModelStoreOwner so scoped ViewModel is supported by default.
By default each Screen has your own ViewModelStoreOwner that doesn't support scoped and each screen will have a unique instance.
// ScreenA
val viewModel1 = viewModel { MyViewModel() }
// ScreenB
val viewModel2 = viewModel { MyViewModel() }
To have ViewModel scoped to a Navigator and share the same instance between screens do:
// ScreenA
val viewModel = rememberNavigatorViewModel<MyViewModel>()
// ScreenB
val viewModel = rememberNavigatorViewModel<MyViewModel>()
If you have a ViewModel scoped to Activity or Fragment do:
// ScreenA
val viewModel = viewModel(viewModelStoreOwner = activityOrFragmentViewModelStoreOwner) { SharedViewModel() }
@HiltViewModel has support now to use scoped ViewModel. To have or to get scoped ViewModel just provide to hilt function a ViewModelStoreOwner
Any update on this PR?
@programadorthi Is is possible for you to release this on your own fork? This is really a big challenge for me..
Sorry guys. I can't. You will have to wait for owners review to check and avoid breaking changes.
I'm really looking forward to this feature
hi, are there any plans to merge this?
@osrl we have a experimental support for it on the new Lifecycle KMP API.