Hello everyone, I'm trying a compose in a simple a...
# compose
j
Hello everyone, I'm trying a compose in a simple app, and I wonder what is a proper way to create ViewModels for composable screens. Atm there are two screens in an app, both of them in one Activity. Navigation is implemented with NavHost. Each screen should have its ViewModel. Should I create both ViewModels in my single Activity or may be create many Activities? Each Activity for one composable screen? I'm using the first approach now, but it can be problematic if for example I would need to pass a runtime arguments to second ViewModel and those arguments should come from first VM. The 'many-activities' approach doens't seem to be correct either, so what is a correct pattern for that?
i
Note that when using
NavHost
, using the
viewModel()
API within a screen will automatically scope that ViewModel to the current
composable
screen.
m
What do you mean by a
composable
screen?
Is it the activity or the UI created by a single composable?
j
Could you provide some docs/articles about
NavHost
automatically scoping the
ViewModel
I can't find anything about it 😕 .
By a
composable
screen I meant each
composable
created in
NavGraphBuilder
, each of them has its
ViewModel
in my usecase.
i
Yes, I mean the
composable
in your
NavHost
❤️ 1
j
Thanks for help 🙂