Skip to content

Issue with focusRestorer and handleDPadKeyEvents #194

@GeorgiBurgazliev

Description

@GeorgiBurgazliev

When you override onKeyPresser with "handleDPadKeyEvents" and you have "focusRestoter" pressing dPag up (triggering on up) results in focusing the first item. If you remove the focus restorer this issue is not reproduced.

Example code:

@Composable
fun Screen() {
 
    val focusManager = LocalFocusManager.current
 
    LazyColumn {
        item {
            repeat(10){
                MyLazyRow(focusManager)
            }
        }
    }
}
 
@Composable
fun MyLazyRow(focusManager: FocusManager) {
 
    val (lazyRow, firstItem) = remember { FocusRequester.createRefs() }
 
    LazyRow(
        horizontalArrangement = Arrangement.spacedBy(20.dp),
        modifier = Modifier
            .focusRequester(lazyRow)
            .focusRestorer {
                firstItem
            },
    ) {
        repeat(10) { index ->
            item {
                val itemModifier = if (index == 0) {
                    Modifier.focusRequester(firstItem)
                } else {
                    Modifier
                }
 
                OutlinedButton(
                    onClick = {
                        focusRequester.saveFocusedChild()
                        println("Button clicked")
                    },
                    content = {
                        Text("My Button $index")
 
                    },
                    modifier = itemModifier
                        .handleDPadKeyEvents(onUp = {
                            focusManager.moveFocus(FocusDirection.Up)
                        }),
                    border = ButtonDefaults.border(
                        border = Border(
                            border = BorderStroke(
                                width = 1.dp,
                                color = Color.Red
                            ),
                            shape = RoundedCornerShape(6.dp)
                        ),
                        focusedBorder = Border(
                            border = BorderStroke(width = 10.dp, color = Color.Green),
                            shape = RoundedCornerShape(6.dp)
                        ),
                        disabledBorder = Border(
                            BorderStroke(
                                width = 1.dp,
                                color = colors.onBackground.copy(0.4f)
                            )
                        ),
                        focusedDisabledBorder = Border(
                            BorderStroke(
                                width = 1.dp,
                                color = colors.onBackground.copy(0.4f)
                            )
                        )
                    ),
                )
            }
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions