I have two screens in my app: SCREEN_A and SCREEN_B
SCREEN_A -> listens for SCREEN_B's result
@Composable
fun ScreenA() {
val result = currentBackStackEntry
?.savedStateHandle
?.getLiveData<String>("resultKey")
?.observeAsState()
result?.value?.let {
Log.d("AABBCCDDEE", "TESTING RESULT")
currentBackStackEntry
?.savedStateHandle
?.remove<String>("resultKey")
}
// ...
}
SCREEN_B -> fires a result and pops itself from the stack
@Composable
fun ScreenB() {
val coroutineScope = rememberCoroutineScope()
LaunchedEffect(key1 = Unit) {
scope.launch {
delay(3000)
navController.previousBackStackEntry
?.savedStateHandle
?.set("resultKey", "TESTING HERE : )")
navController.popBackStack()
}
}
}
The problem is that when the app navigates back from SCREEN_B to SCREEN_A, the result is being fired twice (the string "TESTING RESULT" is being printed twice).
What can I do to avoid that?
I thought that this piece of code would've been enough
currentBackStackEntry
?.savedStateHandle
?.remove<String>("resultKey")