Open jortas opened 2 months ago
It seems that this sort of pattern already exists, but is unfortunately marked as internal. See here
Also to anyone else trying to use the above code, make sure to have the kotlin-parcelize plugin enabled in your common module.
Add below compose to your project and use it like
BackPressHandler { //back pressed
//Do Something
}
@Composable
fun BackPressHandler(
backPressedDispatcher: OnBackPressedDispatcher? =
LocalOnBackPressedDispatcherOwner.current?.onBackPressedDispatcher,
onBackPressed: () -> Unit
) {
val currentOnBackPressed by rememberUpdatedState(newValue = onBackPressed)
val backCallback = remember {
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
currentOnBackPressed()
}
}
}
DisposableEffect(key1 = backPressedDispatcher) {
backPressedDispatcher?.addCallback(backCallback)
onDispose {
backCallback.remove()
}
}
}
It is normal that developers might want a different backHandling depending on the current screen. For that I developed a custom solution for my project, it would be nice to have something like this in the base code.
And the Screen becomes
and on the activity