Having a screen A in which you select a category, ...
# compose
p
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
so it is the correct approach?
and what to do with argument once received?
the second part of my question
i
ViewModels automatically receive all arguments via their
SavedStateHandle
, you don't have to pass them to the ViewModel at all
1
s
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
so, what do you mean?
which is the correct approach then?
maybe can you give a small sample with code for your proposal?