Hi everyone, I have a question about navigation in...
# compose-android
y
Hi everyone, I have a question about navigation in compose, I have one screen which has parameters, When, I navigate to another screen from this screen, I cannot navigate back with parameters. Has someone found a solution for this problem?
p
Don't know exactly what you need but isn't just popping the stack to the respective screen more than enough? Or you mean passing back some result?
y
Yes @Pablichjenkov, I want to this passing back with some result as a parameter
s
Not on my computer atm, but you can do something like:
Copy code
navController.navigate(YourScreen(params)) { popUpTo(YourScreen) { inclusive = true} }
y
Imagine I have one A screen takes 3 parameters, and I navigate to B screen then I want to navigate back. If I use just popUpToBack() no problem navigation works fine, but I want to new feature like From B screen I want to popUpToBack() with some result.
p
There is an API to send back a result using the SavedStateHandle, you have to use that or some other mechanism like an external data source. The input args are immutable for a specific Navbackstackentry
y
I will try to implement send back result API to my logic, I will tell you result later. Thanks for now @Pablichjenkov.
s
I think what you want to do is, before popping or navigating up, set the results on the previous backstack entry's
SavedStateHandle
. Something like this:
Copy code
val handle = navController.previousBackStackEntry?.savedStateHandle

handle["result-key"] = "Result Value"
Then, in the destination for the screen you're going back to, you would want to observe changes to the `SavedStateHandle`:
Copy code
composable<ScreenYouPoppedBackTo>() { backStackEntry ->
  val result = backStackEntry.savedStateHandle
    .getStateFlow<String?>("result-key", initialValue = null)
    .collectAsState()
}
👍 2
j