Status Update
Comments
da...@google.com <da...@google.com> #2
That function located in the hilt-navigation-compose
artifact is not public nor intended for usage with arbitrary factory ViewModelStoreOwner
and SavedStateRegistryOwner
, its used only via the hiltViewModel()
compose functions. The reality is that we can't accept two parameters of each owner since the user might wrongly pass different owners with different lifecycles which would cause trouble.
I think what you are asking is more along the lines of:
@Composable
fun <T> createHiltViewModelFactory(owner: T, args: Bundle) : ViewModelProvider.Factory?
where T : ViewModelStoreOwner, T : SavedStateRegistryOwner, T : HasDefaultViewModelProviderFactory {
return dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.createInternal(
LocalContext.current.unwrap(),
owner,
args,
owner.defaultViewModelProviderFactory,
)
}
where the owner is both a view model store owner and saved state owner. I think we could offer that API if there is a use-case for it...
Note that we want to offer the flexibility of creating the HiltViewModelFactory
in a simpler way, just via a ViewModelStoreOwner
, but it requires decoupling the implicit contract of ViewModelStoreOwner
and SavedStateRegistryOwner
and more specifically allow creating SavedStateHandle
s without forcing a factory subclass, see
zh...@gmail.com <zh...@gmail.com> #3
Yes, that is correct.
Technically, requiring a "SavedStateRegistryOwner that is also a ViewModelStoreOwner" is not unprecedented, viewmodel-savedstate requires it here:
da...@google.com <da...@google.com> #4
With the release of creation extras we can clean this up avoiding the saved state and vm owner disparity and loss of default args, since by just taking a VM owner the matching args and saved state owner will be available in the creation extras.
ku...@google.com <ku...@google.com>
ap...@google.com <ap...@google.com> #5
Branch: androidx-main
commit 0bd253de24bea5e6bc1a85405b7f9b9d365395ac
Author: Kuan-Ying Chou <kuanyingchou@google.com>
Date: Thu Nov 10 17:46:22 2022
Accept HasDefaultViewModelProviderFactory in hiltViewModel()
We excluded ViewModelStoreOwners that's not a NavBackStackEntry.
In this CL we accept it as long as it's a HasDefaultViewModelProviderFactory
as we can now get defaultArgs and savedStateRegistryOwner from
creationExtras.
Test: HiltViewModelComposeTest
Bug:
Relnote: Accept HasDefaultViewModelProviderFactory in hiltViewModel()
Change-Id: I10ab6e8b08e4eb2d27e0d7979e4a4f6663798245
M hilt/hilt-navigation-compose/src/androidTest/java/androidx/hilt/navigation/compose/HiltViewModelComposeTest.kt
M hilt/hilt-navigation-compose/src/main/java/androidx/hilt/navigation/compose/HiltViewModel.kt
M hilt/hilt-navigation-fragment/src/main/java/androidx/hilt/navigation/fragment/HiltNavGraphViewModelLazy.kt
M hilt/hilt-navigation/api/current.txt
M hilt/hilt-navigation/api/public_plus_experimental_current.txt
M hilt/hilt-navigation/api/restricted_current.txt
M hilt/hilt-navigation/src/main/java/androidx/hilt/navigation/HiltNavBackStackEntry.kt
Description
Component used: Compose Version used: 2.38.1 Devices/Android versions reproduced on: Any
As per the code of
The
ViewModelStoreOwner
only works if it is a NavBackStackEntry, but in reality, it should work with any arbitrary SavedStateRegistryOwner.