I'm using navigation on a parent navhost holder, w...
# compose
p
I'm using navigation on a parent navhost holder, which presents two destinations. Screen A can request navigation to B, and B should return a value to A.
Copy code
composable(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:
Copy code
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.
i
previousBackStackEntry?.savedStateHandle
is completely different from the
SavedStateHandle
associated with a particular ViewModel: https://stackoverflow.com/a/76901998/1676363
p
I'm trying to understand the concept, it's not easy. On the other hand, in your solution you propose to do this on ScreenA:
Copy code
val 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?
also, seems to be a very bad practice to ask for a viewmodel of another screen inside a screen
I'm confussed
to make it even more harder, I'm using koin, not hilt and can't find how to do that with koin. Tryed also with LLM's but they can't offer a migration to koin that compiles and can be executed of these lines that get the viewmodel of the other screen in the other composable (thing that sounds really bad, on the other hand)
any help with this please?