Jetpack compose ViewModels - should I have one ViewModel for different api calls?

30 Views Asked by At

Currently, I'm trying to get down basics of jetpack compose, and I'd like to ask you about the view model. Let's say we have an app which will make a few different api calls. As far as I know, before jetpack compose, we could have a few fragments for each api call (because they will serve completely different data). So we had to make separated ViewModel for each fragment, right? But how should I approach ViewModels in jetpack compose, when we can have a single activity, that display many composables? Should I stay with one ViewModel then?

1

There are 1 best solutions below

0
ryankuck On

I would think of a viewModel as being for a specific product purpose. One activity will have one or more view models. Some can be reused by other activities if desired. You can have as many api calls as you want for that product purpose connected to the viewModel.