Open babew opened 3 months ago
I found the following that may shed some light on this. The savedStateHandle that is on the BackStackEntry vs on the ViewModel are intentionally not the same instances with jetpack compose
The important thing to realize is that every ViewModel instance gets its own SavedStateHandle - if you accessed two separate ViewModel classes on the same screen, they would each have their own SavedStateHandle.
So when you call navController.currentBackStackEntry?.savedStateHandle, you aren't actually getting the SavedStateHandle associated with your CreatePostViewModel - if you look at the NavBackStackEntry source code, you'll note that the SavedStateHandle it is returning is for a private ViewModel subclass that is completely independent of any other ViewModels you create.
Therefore if you want to send a result back specifically to your own custom ViewModel (like your CreatePostViewModel), you need to specifically ask for exactly that ViewModel in your other screen
Therefore, I think this is pretty much how compose works vs a koin bug.
Describe the bug I'm triyng to pass result data from one screen to previous screens view model with SavedStateHandle but savedStateHandle.getStateFlow isn't triggered.
Koin module and version:
koin-core:4.0.0-RC1
koin-compose:4.0.0-RC1
koin-compose-viewmodel:4.0.0-RC1
Snippet or Sample project to help reproduce
And the printed log is:
Conclusions: Passing data in SavedStateHandle works as expected because navController.currentBackStackEntry?.savedStateHandle?.get(PHONE_CODE)} starts returning the right result but when I try to use SavedStateHandle in ViewModel injected by Koin, the getStateFlow isn't triggered after navController.previousBackStackEntry?.savedStateHandle?.set(PHONE_CODE, phoneCode).
I can see that the instances of SavedStateHandle from Composable function and ViewModel are different: androidx.lifecycle.SavedStateHandle@f3a6d39 in ViewModel androidx.lifecycle.SavedStateHandle@685023a in the Composable function