3

The below-provided code is a simplified version of my problem.

I want to trigger an action when the device back button is clicked when the keyboard is open.

It is possible to handle the back button click using BackHandler when the keyboard is closed. But I want it even if the keyboard is open.

@Composable
fun BackHandlingWhenKeyboardOpen() {
    val focusManager = LocalFocusManager.current
    BackHandler(
        enabled = true,
    ) {
        // This is not triggered when keyboard is open
        Log.d("TEST_TAG", "Back Handler")
    }
    Box(
        contentAlignment = Alignment.Center,
        modifier = Modifier
            .fillMaxSize()
            .clickable(
                indication = null,
                interactionSource = remember { MutableInteractionSource() },
            ) {
                focusManager.clearFocus()
            }
    ) {
        TextField(
            value = "",
            onValueChange = {},
            keyboardActions = KeyboardActions(
                onDone = {
                    focusManager.clearFocus()
                },
            ),
            keyboardOptions = KeyboardOptions(
                keyboardType = KeyboardType.NumberPassword,
                imeAction = ImeAction.Done,
            ),
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp),
        )
    }
}
Abhimanyu
  • 11,351
  • 7
  • 51
  • 121
  • I asked similar question here yet no answer. I think it's not possible yet. https://stackoverflow.com/questions/71361664/jetpack-compose-intercept-back-button-from-soft-keyboard – Thracian Aug 08 '22 at 10:36
  • Thanks, @Thracian for pointing it out. Please upvote this issue if this helps - https://issuetracker.google.com/issues/241705563 – Abhimanyu Aug 08 '22 at 10:40

0 Answers0