Is it possible to create a ViewModel inside a Comp...
# compose
z
Is it possible to create a ViewModel inside a Compose function? What would be the approach in Compose only app?
a
they’re working on it. Right now it kinda works but there’s a bug on configuration change (state restoration does not work) if I’m not wrong.
z
Then I think wait for a while for that support and go with fragment for now. Thanks for the reply.
a
it actually works. you can use
viewModel
composable function to get it. it will be a ViewModel bind to Activity or Fragment and will survive the screen rotation. the only limitation is that this viewmodel will not be destroyed when you stopped displaying this composable
z
yeah. my concern was to destroy
ViewModel
when the composable is out of the screen. is there any workaround to do that now?
a
no. it will work when we release a navigation integration. in this case every navigation destination will have their own ViewModels scope
👍 1
a
Ah, thank you for the correction 🙇 . I barely remember the ViewModel PR in the sample that got reverted 😄