I’m building an Android app using MVVM, Jetpack Co...
# compose
p
I’m building an Android app using MVVM, Jetpack Compose, and Koin. In one of my screens, I have a parent screen that haves a NavHost. This parent screen contains several sub‑screens, and each sub‑screen is responsible for editing a different part of a large object (with dozens of fields, images, signatures, etc.) which is stored on the parent viewmodel uistate. Each sub‑screen has: • its own ViewModel (logic for making photos, signatures, electing items in complex lists with filters etc...) • its own UI state (
mutableStateOf
for TextFields, booleans, etc.) • callbacks that send the edited data back to the parent Now I need the parent screen to pass the initial data of this large object down to each sub‑screen. The problem is that each sub‑screen has its own ViewModel with its own
mutableStateOf
fields, so those fields start out empty instead of being initialized with the parent’s data. My question is: what is the cleanest architecture for this scenario? Option 1 Pass the initial object as a parameter to each sub‑screen and use a
LaunchedEffect
to push the initial values into the child ViewModel. Option 2 Serialize the object to JSON, pass it through Navigation arguments, and let each child ViewModel read it from the
SavedStateHandle
. Whould be a very huge json. Option 3 Allow the sub‑screens to access the parent ViewModel directly so they can read the initial data from there. This sounds rare. Each subscreen whould have two viewmodels. Option 4 One single shared viewmodel for the parent screen and the subscreens. This is discarded, because the subscreens require an own viewmodel. They do have a lot of mutablestateof for the text fields, a lot of logic and stuff and edit a lot of components and mixing everything in a single viewmodels seems to be an error. Which approach is the cleanest and most idiomatic?