Android compose Modifer onKeyEvent for back pressed with voyager navigation library

64 Views Asked by At

My android application has two compose screens and i am navigating between them with the navigator library

i am attempting to capture the system back key in the second screen which does work however it also seems to be propagated to my first screen as that also exits

here is the compose code i am employing to detect the back key press

val requester = remember { FocusRequester() }
val navigator = LocalNavigator.currentOrThrow

    LaunchedEffect(Unit) {
        requester.requestFocus()
    }

    AndroidView(
        modifier = Modifier
            .focusRequester(requester)
            .focusable()
            .fillMaxSize()
            .onKeyEvent { keyEvent ->
                if (keyEvent.nativeKeyEvent.keyCode == KeyEvent.KEYCODE_BACK) {
                    if (keyEvent.nativeKeyEvent.action == KeyEvent.ACTION_DOWN) navigator.pop()
                    true
                } else true
            },
        onRelease = {

when i press back in my second screen i briefly see the first screen then that exits as well

as i am returning true from the onKeyEvent i was under the impression that would stop propagation of the system back key

i have tried using backhandler to detect back key presses and it does not work

is it not possible to consume the system back key and allow my application to simply return from my second screen to the first?

0

There are 0 best solutions below