android / android/tv-samples

Issue with focusRestorer and handleDPadKeyEvents

Open
#194 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Kotlin
Stars
1.3k
Forks
396
PR merge metrics
No merged PRs in 30d

Description

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)
)
)
),
)
}
}
}
}
```

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the behavior with the Kotlin Compose example in the issue, keeping both focusRestorer and handleDPadKeyEvents enabled. Check what happens when D-pad Up triggers onUp, and consider the issue done when that interaction no longer incorrectly focuses the first item while focus restoration remains enabled.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, kotlin
Domain
mobile-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.