1

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.

1 Answer 1

2

What they mean by no business logic in composables is that the composable function itself should have no business logic. So for example, if you want to hide or show a dialog based on some condition, the condition should be evaluated and stored in the ViewModel. The composable then has an if statement that checks the value to output it or not. The same for interactions- the composable should not decide what to do when you tap a buttton. Your ViewModel should have an onButtonTapped member variable, and the composible sets that function to be called when the button is tapped. But the composable itself has no idea what that function does.

In general I'd say there's very little use for a global ViewModel in Compose or in Views. You have a ViewModel for an Activity, Fragment, or other segment of your screen. But remember the purpose of a ViewModel- it's a transform of the model, removing anything not needed for the particular view and transforming it from whatever makes sense to store at an app level to what is needed for a particular view's display. So generally there isn't anything needed for that at the Activity level if you have a view model at a lower level. Unless you're performing the antipattern of using it for general storage of model data.

Sign up to request clarification or add additional context in comments.

1 Comment

The recently used Navigation3 component allows storing backstack entries of the navigation graph in the ViewModel, see this section of the documentation: developer.android.com/guide/navigation/navigation-3/… So that would probably be an example for a global ViewModel, as this ViewModel needs to live as long as the single Activity lives.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.