Guillaume Lardillier
09/20/2022, 9:04 AMGuillaume Lardillier
09/20/2022, 9:57 AM@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) :
@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 😅