Lets assume I have two screens, ScreenA and Screen...
# compose
u
Lets assume I have two screens, ScreenA and ScreenB. If I toggle a switch to enabled mode in screenB, it should show OptionA : true" in ScreenA. What is the best approach to do this ? I have opted for the MVVM pattern, so will use view models to maintain the state of each screen, but I am not sure how to make each screen's state accessible to the other. I would imagine using some form of datastore. Please advise.
j
You might hoist the state to the parent composable that hosts ScreenA and ScreenB and pass a property and setter into either or both as needed. The state could be in a viewmodel scoped to that parent composable. Alternatively, you could observe the state from a shared database, where updating the database state would be observed by the UI as needed. This would make sense especially if the state isn't transient and should be persisted between app runs.
👍 1
u
What about a more reactive approach?
j
I would expect the hoisted state to be a compose
State
, internally
MutableState
for the parent view model to update with the update function reference, which the UI would react to the update to. You could also do the same with a
MutableStateFlow
and
collectAsState
as well. The database approach would also be reactive as well, assuming an observable datastore.
1