https://kotlinlang.org logo
#compose
Title
# compose
k

Karthick

02/24/2021, 10:15 AM
Any idea how to create viewmodel scoped to composable function, means it will be destroyed once the function disposes.
a

allan.conda

02/24/2021, 10:16 AM
not implemented yet
if you use compose-navigation, however, it will be scoped to the NavBackStackEntry
k

Karthick

02/24/2021, 10:18 AM
wow thanks, i will follow up on issue tracker
1
yes am using navigation will it handle automatically
l

lewis

02/24/2021, 10:18 AM
I thought
androidx.lifecycle.viewmodel.compose.viewModel
did this already?
k

Karthick

02/24/2021, 10:19 AM
ok thanks, will check
l

lewis

02/24/2021, 10:19 AM
Ah, my misunderstanding - it's scoped to the fragment/activity.
a

allan.conda

02/24/2021, 10:43 AM
It’s scoped to LocalViewModelStoreOwner, it’s a bit difficult to debug, but Locals can be overridden locally within the hierarchy
so by default, I think it defaults to either the Activity or Fragment ViewModelStoreOwner. But if you use compose-navigation `composable(key)`function, value is changed somewhere into the NavBackStackEntry.
1
i

Ian Lake

02/24/2021, 10:18 PM
NavHost
swaps out the
LifecycleOwner
,
ViewModelStoreOwner
,
SavedStateRegistryOwner
, and
SaveableStateProvider
to ones that are tied to that specific destination, yes.
👏🏼 1