I get a crash (both Android + JVM) when I try to m...
# compose
a
I get a crash (both Android + JVM) when I try to move focus to a child when a parent is focused Is this a bug or am i doing something wrong? 🧵
This crashes with an StackOverflow:
Copy code
val groupFocusRequester = remember { FocusRequester() }
val childFocusRequester = remember { FocusRequester() }

LaunchedEffect(Unit) {
    groupFocusRequester.requestFocus()
}

Box(
    modifier = Modifier
        .focusRequester(groupFocusRequester)
        .onFocusChanged {
            if (it.isFocused) {
                childFocusRequester.requestFocus()
            }
        }
) {
    Button(onClick = {}, modifier = Modifier.focusRequester(childFocusRequester)) {
        Text("Child")
    }
}
d
Does it do the same if you do the focus requesting on a click instead of via LaunchedEffect ?
a
yes. i changed it to launched effect so it's simpler to reproduce
d
Focus Requester is a weird one that you might wanna keep reproducer to onClick, the system doesn't like focus being requested particularly early E.g. in
FocusRequester.kt
:
Copy code
private const val FocusRequesterNotInitialized = """
   FocusRequester is not initialized. Here are some possible fixes:

   1. Remember the FocusRequester: val focusRequester = remember { FocusRequester() }
   2. Did you forget to add a Modifier.focusRequester() ?
   3. Are you attempting to request focus during composition? Focus requests should be made in
   response to some event. Eg Modifier.clickable { focusRequester.requestFocus() }
"""
a
that is unrelated to what is being discussed
d
Sure, but makes the reproducer closer to what the docs would want.