Sergey Zarochentsev

    Sergey Zarochentsev

    1 year ago
    Hello! I want to set focus on input and show a keyboard when the screen is showed. I want to implement this behavior on a number of screens and I want to reduce code duplication with FocusRequester and LaunchedEffect. Is it correct to create requestFocus modifier like this?
    fun Modifier.requestFocus(requester: FocusRequester = FocusRequester()): Modifier = composed {
        LaunchedEffect(requester) {
            requester.requestFocus()
        }
    
        focusRequester(requester)
    }
    Zach Klippenstein (he/him) [MOD]

    Zach Klippenstein (he/him) [MOD]

    1 year ago
    That looks correct, yes. Whether it’s a pattern that will get you into trouble as you scale, idk. But I’d probably give it a more descriptive name at the very least (eg
    requestFocusImmediately
    )
    Sergey Zarochentsev

    Sergey Zarochentsev

    1 year ago
    @Zach Klippenstein (he/him) [MOD] thanks for your feedback!