Hey guys, need some help, I have the following sce...
# android
j
Hey guys, need some help, I have the following scenario: I have an object that gets populated through multiple screens (I use Compose), what is the right way to do this? • A - Have a ViewModel scoped to the Activity/Fragment and mutate the object in each screen? • B - Pass the object down to each Composable and mutate it in each VM (scoped to the Composable) ? • C - What would be the right way?
😶 1
c
My suggestion is that, for any data that needs to be shared between multiple screens, it should be managed in some kind of “repository layer”, ideally backed by persistent storage (but not strictly necessary). Each screen should then query the Repository itself and observe any changes to that value. So the Repository would expose that value as a
Flow
, which would be something like a reactive DB query, a property in DataStore, or an in-memory value held in a
StateFlow
, for example. Then, when any screen updates that object, the new value gets pushed to all screens reading it
🙌 1
💯 2
j
Make sense as this consolidates the data, and we would only have a single source of truth (with the repository). Thanks!