After updating to compose 1.7, I’m having focus is...
# compose
o
After updating to compose 1.7, I’m having focus issues with an
AndroidView
inside a compose hierarchy. If I call
requestFocus()
on a
FocusRequester
attached to a composable outside of the
AndroidView
, the focus jumps to another view inside an
AndroidView
instead of the composable with
FocusRequester
. The jump size seems related to the number of items inside the composable with
FocusRequester
. E.g. if there are 4 items, it jumps by 4 items, if the composable with
FocusRequester
is a
focusGroup()
, it jumps by 1 item. In case of a multiple-item-jump
ViewGroup.requestChildFocus(child, focused)
is called for every child view. I’ve also tried experimenting with
focusProperties
on the
AndroidView
but no luck. Is there any special setup required since 1.7 or is this a bug? I’ve already filed an issue with a minimal repro, but no response yet. https://issuetracker.google.com/issues/352690889
androidviewfocus.mp4
z
@Ralston Da Silva
r
I took a quick look and it looks like the outer composable which as a focusRequester is not itself focusable. It looks like you want to move focus to the first item when requestFocus() is called. This can be done by making Menu a focusGroup(). This can be done by adding a focusGroup() Modifier after the focusRequester() modifier. Can you try: Menu( modifier = Modifier .fillMaxHeight() .width(64.dp) .focusRequester(menuFocusRequester) .onFocusChanged { hasMenuFocus = it.hasFocus } .focusGroup() )
o
Yes, the goal is to move focus to an item in the menu. I've tried adding
focusGroup()
to
Menu
, that's actually how we have it set up in production, but menu still doesn't get focused. Instead of jumping by 4 items down, the focus jumps by 1 item.
@Ralston Da Silva, just checking in - have you had a chance to look into this more?
r
I haven't had a chance to look at this. Did you end up fixing this issue?
o
Nope, I couldn't think of anything else to try without debugging compose itself. And since we ultimately want to migrate all view-based components to compose, we invested some time in the migration. That's how we encountered this other issue https://kotlinlang.slack.com/archives/CJLTWPH7S/p1726579555988649.