0

Should I always initialize a viewmodel in the Activity, before onCreate() even, if it is needed later in the flow?

I do not know and am concerned if initialization of all the viewmodels right at the start will hinder the app's performance. Most of them won't be executing any function at that time, just initialize and pass.

1 Answer 1

1

There is no clear-cut rule, but if you structure your app into Screens, and have roughly one view model per screen, then a natural place to retrieve a view model instance is at the start of each screen composable.

You may wrap the screens in Routes to use those as navigation destinations, then you can let the route retrieve the view model and only pass the view model's properties and callbacks to the screen. That has the added benefit that your screen's parameters stay agnostic of the view model, which makes automatic testing and previewing much easier. You should not, however, pass entire view model instances around. That doesn't work well with Compose.

This seems like a reasonable upper bound to how deep in your composable hierarchy you should move the view model instantiation. I would also not recommend doing it much earlier to prevent unnecessary instantiations (certainly not in the activity, unless the view model targets the activity specifically).

You can also have a look at the official example app Now in Android and see how they have done it there.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.