https://kotlinlang.org logo
Title
j

jasu

05/14/2022, 6:50 AM
I’m facing weird issue with software keyboard and textField focus. I’ve a note textfield in “note-detail” screen which has got
Modifier.clearFocusOnKeyboardDismiss
custom modifier which clears the textfield focus once softkeyboard is dismissed. It works fine until I move to “note-share” composable. Once I move to note-share composable and come back to note detail this time clearFocusOnKeyboardDismiss stops working, it doesn’t clears the focus. Refer this video.
re-asking! 😓 anyone has idea about it?
z

Zach Klippenstein (he/him) [MOD]

05/15/2022, 8:28 PM
What does your modifier look like? And what version of compose are you using?
j

jasu

05/16/2022, 5:48 AM
Modifier to the note textfiled
NoteTextField(
    textFieldValue = viewmodel.noteTextFieldState,
    onNoteChange = viewmodel::setOnNoteChange,
    isFocused = viewmodel.currentlyEditing == Note,
    onFocusChanged = {
        if (it.isFocused) {
            viewmodel.setCurrentlyEditingState(Note)
        }
    },
    shouldPadToNavigationBars = viewmodel.noteBackgroundChangerBottomSheetVisible,
    onSoftKeyboardDismissed = {
        viewmodel.setCurrentlyEditingState(None)
    },
    modifier = Modifier
        .padding(
            start = horizontalStartSpacing,
            end = horizontalEndSpacing,
            top = 8.dp
        )
        .fillMaxSize()
        .imePadding(),
    editable = textFieldEditable,
)
compose_version = ‘1.2.0-alpha08’
z

Zach Klippenstein (he/him) [MOD]

05/16/2022, 4:47 PM
That looks like a composable, not a modifier, and I don’t see
clearFocusOnKeyboardDismiss
mentioned anywhere. That said, a parameter named
isFocused
seems suspicious, since the actual focus state of the field is determined and owned by the focus system itself, you can’t hoist that state. Can you share the implementation of
NoteTextField
as well?
j

jasu

05/17/2022, 4:50 AM
heyy any update on this one?
z

Zach Klippenstein (he/him) [MOD]

06/09/2022, 4:49 PM
Sorry, i got busy with a conference and this fell through the cracks. There are a couple places here where I could see race conditions happening, and again having two sources of truth for focus seems like something that will just produce all kinds of fun bugs. Without stepping through the code for your particular project I’m not sure exactly what’s going wrong, but I’d start by fixing those issues first..