Pablo
08/07/2025, 5:19 PMcomposable(AppScreen.ScreenA.name) {
ScreenA(onNavigateToScreenB = {navController.navigate(AppScreen.ScreenB.name)})
}
composable(AppScreen.ScreenB.name) {
ScreenB(onDataCaptured = { capturedData ->
navController.previousBackStackEntry?.savedStateHandle?.set(AppConstants.ARG_DATA_KEY, capturedData)
navController.popBackStack()
}
)
}
I did that, and on ScreenA viewmodel init, I'm collecting savedStateHandle arguments like this:
viewModelScope.launch {
savedStateHandle
.getStateFlow<String?>(AppConstants.ARG_DATA_KEY, null)
.filterNotNull()
.distinctUntilChanged()
.collect { capturedData->
updateData(capturedData)
}
}
But the collect is never called when the popBackStack is called with the new value written on previousBackStackEntry?.savedStateHandle. I'm using Koin and MVVM. After doing a research I can't find an elegant way to solve this.Ian Lake
08/07/2025, 5:23 PMpreviousBackStackEntry?.savedStateHandle
is completely different from the SavedStateHandle
associated with a particular ViewModel: https://stackoverflow.com/a/76901998/1676363Pablo
08/07/2025, 5:33 PMval previousBackStackEntry = remember(it) {
navController.previousBackStackEntry!!
}
val previousViewModel = hiltViewModel<CreatePostViewModel>(previouslyBackStackEntry)
previousViewModel.savedStateHandle?.set("result", "this is result")
But ScreenA and ScreenB doesn't have access to the navcontroller, it is on the parent screen that holds the navhost and manages navigation. And passing down the navcontroller to the routes seems to be a bad practice. What should I do here?Pablo
08/07/2025, 6:03 PMPablo
08/07/2025, 6:03 PMPablo
08/07/2025, 6:45 PMPablo
08/08/2025, 8:49 AM