Using SavedStateHandle in the ViewModel
See original GitHub issueHi there, thanks for this great library!
I’m trying to use navArgsDelegate
to pass the arguments and get them in the ViewModel. While it’s pretty straightforward to extract the arguments using argsFrom(savedStateHandle)
, I can’t figure out how to properly inject SavedStateHandle
in my ViewModel. I’m using koin for DI.
This is how I inject the ViewModel into my Composable:
@Destination(navArgsDelegate = ProfileScreenNavArgs::class)
@Composable
fun ProfileScreen(viewModel: ProfileViewModel = getViewModel()) {}
and that’s how I declare the ViewModel:
viewModel { ProfileViewModel(savedStateHandle = what instance of SaveStateHandl should I inject here?)}
Is it even possible using Koin? Thanks again!
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Saved State module for ViewModel - Android Developers
The SavedStateHandle class is a key-value map that allows you to write and retrieve data to and from the saved state through the...
Read more >How do I use the new Saved State Module of ViewModel
For using Saved State module for View Model you have to add the androidx.lifecycle:lifecycle-viewmodel-savedstate dependency to your project ...
Read more >ViewModels with Saved State, Jetpack Navigation, Data ...
ViewModels handle the onSaveInstanceState case with the SavedStateHandle module. You can scope a ViewModel to a Jetpack Navigation NavGraph for ...
Read more >ViewModel and SavedStateHandle: always retain state
For anything not stored in a LiveData you want to retain, use savedStateHandle.set(KEY, VALUE) / savedStateHandle.get(KEY) (similar to a Map or ...
Read more >ViewModel With SavedState in Android - GeeksforGeeks
The data is stored using savedStateHandle.set(“Key”,”Value”). The saved state handle.get live data(“key”) method is used to retrieve ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yes! DestinationsNavigator’s purpose is just to wrap NavController and invert the dependency, but you can still use NavController everywhere!
There is an extension function “navigateTo” which also takes a Direction (like DestinationsNavigator)
ohhh, I can get an instance of the
NavController
in my composable, that’s very good to know! Thanks, @raamcosta for this explanation, appreciate it!