Rafael Costa
04/27/2024, 9:45 PM2.8.0-alpha06
. I haven't found the root cause and I don't have a lot of time, but I have a case where the SavedStateHandle is empty where a nav arg was sent.
If I just go back to 2.8.0-alpha05
, everything works well again.
I found that the bundle on NavBackStackEntry does have all arguments as expected, so it's something related specifically to SavedStateHandle.
Would report an issue, but tbh I am really short on time 😬Rafael Costa
04/27/2024, 9:45 PMIan Lake
04/27/2024, 10:28 PMviewModel()
call), so I'd check that first to rule out any issue on the Navigation side. Otherwise, worth tracing whatever DI system (if any) you're using to provide your ViewModelsRafael Costa
04/28/2024, 7:40 AMviewModel
function, but still something was changed here.
Is there any intentional breaking change in this version?Rafael Costa
04/28/2024, 7:46 AMviewModel
function:
@Composable
inline fun <reified VM : ViewModel> viewModel(
viewModelStoreOwner: ViewModelStoreOwner = checkNotNull(LocalViewModelStoreOwner.current) {
"No ViewModelStoreOwner was provided via LocalViewModelStoreOwner"
},
savedStateRegistryOwner: SavedStateRegistryOwner = LocalSavedStateRegistryOwner.current
): VM {
return androidx.lifecycle.viewmodel.compose.viewModel(
viewModelStoreOwner = viewModelStoreOwner,
factory = ViewModelFactory(
owner = savedStateRegistryOwner,
defaultArgs = (savedStateRegistryOwner as? NavBackStackEntry)?.arguments,
//remove this line if you're not using Dependency injection
dependencyContainer = [ACCESS YOUR DEPENDENCIES GRAPH HERE SOMEHOW],
)
)
}
Rafael Costa
04/28/2024, 7:48 AMAbstractSavedStateViewModelFactory
to which I pass those defaultArgs
Rafael Costa
04/28/2024, 10:49 AMviewModelStoreOwner
(which was the NavBackStackEntry
).
With version alpha06 and above, I need to use that NavBackStackEntry
on both the viewModelStoreOwner
and the SavedStateRegistryOwner
. This does sort of make sense to me, I just wonder how it worked before. I'm guessing by providing the ViewModelStoreOwner
, even if the SavedStateRegistryOwner
was not correct, it wouldn't matter since the same VM would be returned.Ian Lake
04/29/2024, 3:46 AMCreationExtras
version of create
, which pulls the arguments from the ViewModelStoreOwner who implements HasDefaultViewModelProviderFactory
(which Activity, Fragment, NavBackStackEntry) etc. all implement). The parameters you pass to the constructor are really only for the compat story if you are using a new version of Lifecycle with an old version of older librariesIan Lake
04/29/2024, 3:48 AMIan Lake
04/29/2024, 3:49 AMAbstractSavedStateViewModelFactory
since Lifecycle 2.5. The CreationExtras
approach has been enough since thenRafael Costa
04/29/2024, 8:19 AM