https://kotlinlang.org logo
#compose
Title
# compose
p

Pablo

03/17/2024, 3:56 PM
Having a screen A in which you select a category, how can I pass the category variable to the next screen B? I'm using navigation and viewmodel, so I suposse I should pass the argument using navigation arguments, and then, what should I do with the parameter? should it be passed to the B screen viewmodel in the viewmodel constructor as a parameter? or Which is the correct way to do this?
Or maybe the correct approach is to have a general application state where to store things that must be shared between screens, for example, the current selected category?
p

Pablo

03/17/2024, 4:27 PM
so it is the correct approach?
and what to do with argument once received?
the second part of my question
i

Ian Lake

03/17/2024, 5:40 PM
ViewModels automatically receive all arguments via their
SavedStateHandle
, you don't have to pass them to the ViewModel at all
1
s

sindrenm

03/17/2024, 7:49 PM
Just note that if your category type is more complex than a string, than you won't be able to pass it in it's entirety; you'd instead want to pass something like a category ID or something, then get that category ID from the aforementioned
SavedStateHandle
in Screen B's view model where you'd also get the category based on that ID.
p

Pablo

03/17/2024, 11:38 PM
so, what do you mean?
which is the correct approach then?
maybe can you give a small sample with code for your proposal?