Open GvHarish9894 opened 4 months ago
It's necessary.
I did a custom workaround for passing data between composables. One composable sets the result and another composable can fetch it by its key. After the result consumed, it is cleared from the memory.
// NavigationResult.kt
private val screenResults = hashMapOf<String, Any?>()
fun <T: Any> Navigator.getScreenResult(key: String): T? {
val result = screenResults[key]
screenResults[key] = null
return result as? T
}
fun Navigator.setScreenResult(key: String, result: Any) {
screenResults[key] = result
}
class ScreenA : Screen {
@Composable
override fun Content() {
val navigator = LocalNavigator.current
LaunchedEffect(Unit) {
val screenResult = navigator?.getScreenResult<String>("MY_RESULT")
println("Screen result is $screenResult")
}
Text(
modifier = Modifier.clickable { navigator?.push(ScreenB()) },
text = "ClickMe"
)
}
}
class ScreenB : Screen {
@Composable
override fun Content() {
val navigator = LocalNavigator.current
BackHandler(
enabled = true,
onBack = {
navigator?.setScreenResult("MY_RESULT", "RESULT")
navigator?.pop()
}
)
}
}
Feature need to pass data to previous composable screens like traditional android navigation library previousBackStackEntry kind of function