I didn't notice it in the recipe repo, but is ther...
# compose
e
I didn't notice it in the recipe repo, but is there a way to get a NavEntry scoped SavedStateHandle in nav3?
i
Do you mean magically filling in the SavedStateHandle from the NavKey? That's generally not a pattern you'll see in Nav3 at all, instead preferring directly injecting the NavKey into your ViewModel's constructor: https://issuetracker.google.com/issues/420932904#comment4
But if you mean scoping ViewModels to individual screens, there is the
lifecycle-viewmodel-navigation3
artifact that is part of Lifecycle 2.10.0: https://developer.android.com/jetpack/androidx/releases/lifecycle#2.10.0
g
You also have some recipes for hilt/koin for the args that quite straightforward
btw @Ian Lake "That's generally not a pattern you'll see in Nav3 at all" why is that so?
i
You did read the comment I linked right?
g
haha not at all and just did 😬
i
For other folks:
Unlike Nav2, Nav3 does not require keys to be saveable, but that is a requirement for SavedStateHandle, so there is already some mismatch between those systems. In addition, even if you are using a saveable key, every ViewModel is doubling the amount of saved instance state you are storing - once for your back stack and once in the SavedStateHandle, which is unnecessary.
🔥 1
e
I don't actually use ViewModel, but in navigation-compose I would get a SavedStateHandle from the nav entry
i
Maybe you should start with the why you are doing something first before diving into the how
Because that API wasn't useful within a screen itself (you already had access to the arguments in the entry anyways), but was more used for returning results by writing into the previous screen's SavedStateHandle. And we have recipes for how to return results if that is the "why" you are looking for equivalents in Nav3: https://github.com/android/nav3-recipes?tab=readme-ov-file#returning-results
âž• 1
e
I didn't use it often, but it was either for result returning, or for cases where I had business logic outside of a Compose context and couldn't use
rememberSaveable