Hello, I have a little question regarding focus ma...
# compose
g
Hello, I have a little question regarding focus management. As requested (sorry for that), everything is in the following thread 🧵
🧵 1
🧵 1
When I am using these 2 composables, if I click on the back button, the app is closing, which is expected.
Copy code
@Composable
fun Greeting(modifier: Modifier = Modifier, name: String) {
    val focusRequester by remember { mutableStateOf(FocusRequester()) }

    Container(modifier = modifier) {
        Column(Modifier.padding(start = 24.dp)) {
            Button(onClick = { /*TODO*/ }) {
                Text("1")
            }
            Button(modifier = modifier.focusRequester(focusRequester), onClick = { /*TODO*/ }) {
                Text("2")
            }
            Button(onClick = { /*TODO*/ }) {
                Text("3")
            }
            Button(onClick = { /*TODO*/ }) {
                Text("4")
            }
        }
    }

    LaunchedEffect(Unit) {
        focusRequester.requestFocus()
    }
}

@Composable
fun Container(modifier: Modifier = Modifier, content: @Composable () -> Unit) {
    val focusManager = LocalFocusManager.current
    Box {
        Box {
            content()
        }
    }
}
But, if I make a change on the Container composable (I set it as focusable) :
Copy code
@Composable
fun Container(modifier:Modifier = Modifier, content: @Composable () -> Unit) {
    val focusManager = LocalFocusManager.current
    Box {
        Box(modifier = Modifier.focusable()){
            content()
        }
    }
}
I have to press the back button twice to have the application to exit (on the first click, it removes the focus, and on the second click, it exit the app). It seems strange to me that the back button is interfering with focus management, but I suppose that once you set your composable focusable, you have to handle the back button manually ? All I can think of is that when you do not set any Modifier, the default behavior is
focusable(false)
, that would explain the different behavior in both cases, and make sense in mobile perspective. Thanks for your help in understanding that 😅