You can obtain the viewModel within your activity so it's tied to the lifecycle of your activity instead of the shorter lifecycle of your composable
i
Ink
05/16/2021, 4:20 PM
Thanks, I thought about it. I wonder can I create ViewModel for only first initialization of my @Composable
l
Lilly
05/16/2021, 4:25 PM
I'm not using jetpack's viewmodels but as far as I know, you have 2 options to achive this.
1. hoist the instantiation -> instantiate your viewmodel before you call your composable and pass the viewmodel to your composable
2. if you want to do something te first time, your composable enters you can achieve this with
DisposableEffect
but within this scope, you are not able to call @Composable functions, so I doubt you can instantite your viewmodel here.
Lilly
05/16/2021, 4:28 PM
Btw. there is a #compose channel where you can ask those questions 🙂