In a previous StackOverflow question it was recommended that each Activity
should have a single ViewModel
. If there are different 'sections' on the screen which require different model data, then this should be handled with multiple LiveData
in single ViewModel
. https://stackoverflow.com/a/54482736/15597975
With Compose now released, is it ok to have different ViewModels for different 'sections' on the screen i.e. each section is represented as a different composable
and each composable has it's own ViewModel
with own odel LiveData
?
We recommend having 1 ViewModel
per Screen and then passing down the state from there. This makes Composables more reusable and testable because your Composables aren't tied to a particular ViewModel, just the state they require.
@Composable
fun MyScreen(viewModel: ViewModel) {
Column {
TopPart(viewModel.topState)
BottomPart(viewModel.bottomState)
}
}
@Composable
fun TopPart(topState: TopState) {}
@Composable
fun BottomPart(bottomState: BottomState) {}