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),
)
}
}