nglauber
05/19/2021, 7:13 PMA which instantiate my ViewModel using viewModel() function. I want to get the same instance of my ViewModel in screen B after call navController.navigate("B") …itnoles
05/19/2021, 7:21 PMnglauber
05/19/2021, 7:22 PMIan Lake
05/19/2021, 7:23 PMnglauber
05/19/2021, 7:23 PMIan Lake
05/19/2021, 7:24 PMpreviousBackStackEntry applies to any kind of ViewModelIan Lake
05/19/2021, 7:26 PMviewModel() method that takes a ViewModelStoreOwner (i.e., that previousBackStackEntry object)? It just always uses the LocalViewModelStoreOwner.currentIan Lake
05/19/2021, 7:26 PMIan Lake
05/19/2021, 7:27 PMviewModel() is just a one liner around ViewModelProvider(owner, factory).get()nglauber
05/19/2021, 7:28 PMIan Lake
05/19/2021, 7:34 PMB and that ViewModel won't exist anymoreIan Lake
05/19/2021, 7:34 PMnglauber
05/19/2021, 7:35 PMIan Lake
05/19/2021, 7:35 PMSavedStateHandle of the previous destination (which only contains information that survives process death and recreation): https://developer.android.com/guide/navigation/navigation-programmatic#returning_a_resultIan Lake
05/19/2021, 7:36 PMIan Lake
05/19/2021, 7:37 PMA. Of course, your own custom ViewModel could have its own SavedStateHandle - that same SavedStateHandle will be redelivered to the new instance upon recreationIan Lake
05/19/2021, 7:38 PMnglauber
05/19/2021, 7:42 PMSavedStateHandle correctly in my ViewModel both screen will restore the data correctly, right?nglauber
05/19/2021, 7:44 PMpreviousBackStackEntry.
Lifecycle API nor Hilt can solve this right now…Ian Lake
05/19/2021, 7:48 PMA can be saved into a SavedStateHandle it owns, then yep, it'll be restored properly whether it gets created from Screen B or Screen AIan Lake
05/19/2021, 7:49 PMnglauber
05/19/2021, 7:57 PMpreviousBackStackEntry? It’s a Bundle 🤔Ian Lake
05/19/2021, 8:01 PMA, you did val vm: MyViewModel = viewModel(), screen B could do val vm: MyViewModel = ViewModelProvider(navController.previousBackStackEntry).get() to get that same ViewModel instancenglauber
05/19/2021, 8:04 PMIan Lake
05/19/2021, 8:04 PMnglauber
05/19/2021, 8:05 PMViewModelProvider expects a ViewModelStoreOwner not a NavBackstackEntry 😕Ian Lake
05/19/2021, 8:07 PMNavBackStackEntry implements ViewModelStoreOwnernglauber
05/19/2021, 8:08 PMIan Lake
05/19/2021, 8:09 PM!! - the nullability is the problem, not the classnglauber
05/19/2021, 8:11 PMnglauber
05/19/2021, 8:19 PMIan Lake
05/19/2021, 8:21 PMIan Lake
05/19/2021, 8:26 PMval vm: MyViewModel = viewModel(navController.previousBackStackEntry!!)nglauber
05/19/2021, 9:51 PMkey param, or sharedViewModel(), or some scope…Ian Lake
05/19/2021, 9:59 PMViewModelStoreOwner isnglauber
05/19/2021, 10:06 PMviewModel<T>() will create a new instance and sharedViewModel<T>() will check if there’s an instance of the ViewModel, return it.Ian Lake
05/19/2021, 10:28 PMviewModel() already avoids creating a new ViewModel if the LocalViewModelStoreOwner has already created that ViewModel - it just returns the previously existing instanceIan Lake
05/19/2021, 10:28 PMLocalViewModelStoreOwner, there's many, many possible owners that might be appropriate