Can I create `viewModel` scoped to a `composable` ...
# compose
z
Can I create
viewModel
scoped to a
composable
using
navigation-compose
?
i
That's what you get already when you use
viewModel()
inside those composables
n
@Ian Lake correct me if I'm wrong but AFAIK that way it will be scoped to the Fragment/Activity from where you call
setContent
, since it has to be tied to a
ViewModelStoreOwner
i
You are indeed wrong 😄 Navigation provides its own
ViewModelStoreOwner
for each
NavBackStackEntry
as per the docs: https://developer.android.com/guide/navigation/navigation-programmatic#navbackstackentry
NavHost
overrides the ambients so that
viewModel()
within the composable uses that scoped owner
👍 1
This is mentioned in the Navigation Compose release notes: https://developer.android.com/jetpack/androidx/releases/navigation#compose-1.0.0-alpha01
👍 1
This ability to scope a ViewModel to individual composables is indeed a separate problem that is not yet solved, you are right about that part though
n
🎊 Ahh, thanks a lot for the clarification, didn't know about this 😁
z
I was reading the documentation and found nothing there about viewmodel. Now I am clear. thanks @Ian Lake