I’m working on Android and I have some doubts about how to properly manage ViewModels depending on the type of UI I’m using (XML with Activities/Fragments vs Jetpack Compose).
Currently, my main experience is with Jetpack Compose, where I follow these best practices:
Not passing the ViewModel directly to each Composable, but only to the main Composable of the screen.
Composables should receive only states and callbacks, without business logic.
However, when working with XML and Views, it’s more common to use global ViewModels (activityViewModels) because the architecture is based on Activities and Fragments. This makes me confused about how to apply these best practices in Compose.
My specific questions are:
In Compose, is it also recommended to use global ViewModels for state management, or is it better to keep local ViewModels per screen and pass states and callbacks to the Composables?
In which cases does it make sense to use a global (or singleton) ViewModel in Compose?
What is the correct way to abstract shared states between multiple screens in Compose without violating the idea that Composables should be “stateless”?
Any code examples showing best practices in both XML and Compose would be very helpful to understand the transition between both paradigms.
Thank you in advance for any guidance or practical examples.