Tgo1014
04/29/2022, 2:13 PMAdam Powell
04/29/2022, 2:16 PMTgo1014
04/29/2022, 2:16 PMAdam Powell
04/29/2022, 2:32 PMLocalViewModelStoreOwner
and multiplex them into a host viewmodel; this is how androidx navigation-compose does itTgo1014
04/29/2022, 2:34 PMLocalViewModelStoreOwner
right? I need to check how the navigation does it thenAdam Powell
04/29/2022, 2:37 PMLocalViewModelStoreOwner
fetch a container ViewModel that you write for this purpose. Then use a RememberObserver
to remove+clear the ViewModelStore
when a page leaves the composition if the host activity isChangingConfiguration
isn't true
RememberObserver
directly instead of DisposableEffect
is because you need to hook onAbandoned
too; ViewModels are created during composition and need to be cleaned up if the composition they were created in doesn't commitTgo1014
04/29/2022, 2:42 PMAdam Powell
04/29/2022, 2:45 PMTgo1014
04/29/2022, 2:52 PMhilt
? Like specifying some key would get another view model and all calls for the same key would return the same? Is this even possible? hahaAdam Powell
04/29/2022, 4:01 PMjulioromano
04/29/2022, 6:00 PMhave each of your pages provide@Adam Powell (I think I have an English problem here) can you explain with different words what do you mean by “multiplex them into a host viewmodel” ? Thanks!and multiplex them *into a host viewmodel*; this is how androidx navigation-compose does itLocalViewModelStoreOwner
Adam Powell
04/29/2022, 6:39 PMViewModelStore
per page, each one establishes a namespace and scope for viewmodels obtained beneath it. You need a place to store those `ViewModelStore`s and make sure they're retained. Using another ViewModel
is the most straightforward way to do this since it already provides all of the facilities and guarantees you want. You'll create your own ViewModel
that stores a map of some key that is unique for each page to a ViewModelStore
, and you'll need to manage the addition and removal of these `ViewModelStore`s from that map. Then you'll need to provide the correct ViewModelStore
via LocalViewModelStoreOwner
to the content of each page.