Search code examples
androidandroid-jetpack-composetextfieldandroid-softkeyboardandroid-compose-textfield

Keyboard flickering when programmatically hidden in Jetpack Compose


When I programmatically hide the keyboard which is focused on the TextField, it flickering. How to get rid of this?

enter image description here

val keyboardController = LocalSoftwareKeyboardController.current
var text by rememberSaveable { mutableStateOf("") }
TextField(
    modifier = Modifier.fillMaxWidth(),
    value = text,
    keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
    keyboardActions = KeyboardActions(
        onDone = {
            keyboardController?.hide() // Causes flickering
        }
    ),
    onValueChange = {
        text = it
    }
)

I also noticed that sometimes flicker occurs when you open the previous screen. Is it possible to see somewhere what exactly causes the keyboard to open and close?

POCO X3 NFC, Android 12, MIUI Global 14.0.2

androidx.compose:compose-bom:2023.08.00


Solution

  • I guess this is a Jetpack Compose bug on Xiaomi devices. I opened an issue in Google Issue Tracker: https://issuetracker.google.com/issues/296001700

    I found that the bug persists even on a new clean project. I also accidentally discovered a similar bug in the ChatGPT app (created using Compose):

    enter image description here

    If I manually remove focus from EditText bug appears less frequently. When closing keyboard, it flickering approximately in 4 out of 10 cases.

    val localFocusManager = LocalFocusManager.current
    var text by rememberSaveable { mutableStateOf("") }
    TextField(
        modifier = Modifier.fillMaxWidth(),
        value = text,
        keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
        keyboardActions = KeyboardActions(
            onDone = {
                localFocusManager.clearFocus()
            }
        ),
        onValueChange = {
            text = it
        }
    )
    

    But it was not possible to completely get rid of him. I have already tried all possible solutions.

    Feel free to comment in Google Issue Tracker if you have similar bug (you can test it using official ChatGPT app on Xiaomi device).

    My device is POCO X3 NFC, Android 12, MIUI Global 14.0.2