question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using SavedStateHandle in the ViewModel

See original GitHub issue

Hi 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:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
raamcostacommented, Feb 10, 2022

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)

0reactions
mykola-tarchanyncommented, Feb 10, 2022

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found