uli
10/01/2024, 11:32 AMkoinNavViewModel
were the only available piece of information on the whole internet is from the online documentation:
koinNavViewModel() - inject ViewModel instance + Navigation arguments data (if you are using Navigation API)
And from KDoc
Resolve ViewModel instance with Navigation NavBackStackEntry as extras parameters
I do not have the slightest idea what that might mean nor do I understand if both claims are the same. Well, I have ideas but none of them turned out to be what the method does.
Same questions has been asked before but just before the final answer the thread dies :-)
https://kotlinlang.slack.com/archives/C67HDJZ2N/p1680127134580369
Most surprising, comparing the source code of koinViewModel
and koinNavViewModel
I could not find any difference. Well koinViewModel
has a default parameter of defaultExtras()
while koinNavViewModel
has a default parameter of defaultNavExtras
. But those two are finnaly 100% identical.
Thanks for shedding any light on
1. Precondition: when and where can the method be used. A.k.a can ‘if you are using Navigation API’ be further specified. Do I need to be inside a NavGraphBuilder? Just in a composable that is nested in any nav graph?
2. What does it do? A.k.a can ‘instance with Navigation NavBackStackEntry as extras parameters’ be further specified. I expected a view model constructor parameter of type NavBackStackEntry to be automatically injected by koin. But that seems as if it does not work.
Maybe a small example would be really helpful.arnaud.giuliani
10/15/2024, 1:52 PMextras
arnaud.giuliani
10/15/2024, 1:53 PMuli
10/15/2024, 2:46 PMuli
10/15/2024, 3:13 PMdefaultNavExtras
the expected implementation?
fun defaultNavExtras(viewModelStoreOwner: ViewModelStoreOwner): CreationExtras = when {
viewModelStoreOwner is HasDefaultViewModelProviderFactory -> viewModelStoreOwner.defaultViewModelCreationExtras
else -> CreationExtras.Empty
}
arnaud.giuliani
10/16/2024, 7:01 AM