min
07/16/2026, 12:28 PMYou can useDoesn’t this contradictto scope a ViewModel directly to the call site of a composable. […] When the composable that owns the`ViewModelStoreOwner` leaves the composition, the associatedrememberViewModelStoreOwner()is cleared and theViewModelStoreis destroyed.ViewModel
UseBecause compositions are dropped on configuration change, which means theto create a lifecycle-aware store that survives configuration changes.rememberViewModelStoreOwner()
ViewModel would be destroyed?min
07/16/2026, 12:28 PMmin
07/16/2026, 12:31 PM@Composable
fun RememberViewModelStoreOwnerSample() {
val scopedOwner = rememberViewModelStoreOwner()
CompositionLocalProvider(LocalViewModelStoreOwner provides scopedOwner) {
val viewModel = viewModel { TestViewModel("scoped_data") }
}
}
And doing the following:
@Composable
fun RememberViewModelStoreOwnerSample() {
val viewModel = viewModel(
viewModelStoreOwner = rememberViewModelStoreOwner()
) {
TestViewModel("scoped_data")
}
}