https://kotlinlang.org logo
#compose
Title
# compose
m

Marko Novakovic

11/22/2021, 4:50 PM
Copy code
@Composable
private fun MyInputField(
    modifier: Modifier = Modifier,
    text: String,
    onTextChanged: (String) -> Unit,
) {
    val focusRequester = remember { FocusRequester() }
    LaunchedEffect(Unit) {
        focusRequester.requestFocus()
    }

    OutlinedTextField(
        modifier = modifier.focusRequester(focusRequester),
        label = { Text(text = "some text") },
        value = text,
        onValueChange = onTextChanged,
    )
}
sometimes keyboard is show and sometimes it is not what that depends on?
d

Dmitrii Smirnov

11/23/2021, 7:39 AM
why do you remember
focusRequester
?
d

divid3d

11/23/2021, 8:30 AM
Why shouldn't he?
m

Marko Novakovic

11/23/2021, 8:48 AM
I think that you should, error you get about
FocusRequester
tells you to remember it
d

Dmitrii Smirnov

11/23/2021, 9:05 AM
Copy code
val focusRequester = FocusRequester()
    
    TextField(
    ...
    modifier = modifier
        .focusRequester(focusRequester)
    )

    if (requestFocus) {
        DisposableEffect(Unit) {
            focusRequester.requestFocus()
            onDispose { }
        }
    }
that works for me
3 Views