@@ -63,7 +63,15 @@ import androidx.compose.runtime.saveable.rememberSaveable
6363import androidx.compose.runtime.setValue
6464import androidx.compose.ui.Alignment
6565import androidx.compose.ui.Modifier
66+ import androidx.compose.ui.focus.FocusRequester
67+ import androidx.compose.ui.focus.focusRequester
6668import androidx.compose.ui.graphics.vector.rememberVectorPainter
69+ import androidx.compose.ui.input.key.Key
70+ import androidx.compose.ui.input.key.KeyEventType
71+ import androidx.compose.ui.input.key.isShiftPressed
72+ import androidx.compose.ui.input.key.key
73+ import androidx.compose.ui.input.key.onKeyEvent
74+ import androidx.compose.ui.input.key.type
6775import androidx.compose.ui.platform.ComposeView
6876import androidx.compose.ui.platform.LocalContext
6977import androidx.compose.ui.semantics.CustomAccessibilityAction
@@ -160,6 +168,7 @@ class FabMenuDemoFragment : DemoFragment() {
160168fun FabMenuDemoContent (onItemClick : (String ) -> Unit , onExpandedChange : (Boolean ) -> Unit ) {
161169 val listState = rememberLazyListState()
162170 val fabVisible by remember { derivedStateOf { listState.firstVisibleItemIndex == 0 } }
171+ val focusRequester = remember { FocusRequester () }
163172
164173 Box {
165174 val items = remember {
@@ -194,7 +203,8 @@ fun FabMenuDemoContent(onItemClick: (String) -> Unit, onExpandedChange: (Boolean
194203 .animateFloatingActionButton(
195204 visible = fabVisible || fabMenuExpanded,
196205 alignment = Alignment .BottomEnd ,
197- ),
206+ )
207+ .focusRequester(focusRequester),
198208 checked = fabMenuExpanded,
199209 onCheckedChange = { fabMenuExpanded = ! fabMenuExpanded },
200210 ) {
@@ -229,7 +239,24 @@ fun FabMenuDemoContent(onItemClick: (String) -> Unit, onExpandedChange: (Boolean
229239 )
230240 )
231241 }
232- },
242+ }
243+ .then(
244+ if (i == 0 ) {
245+ Modifier .onKeyEvent {
246+ // Navigating back from the first item should go back to the FAB menu button.
247+ if (
248+ it.type == KeyEventType .KeyDown &&
249+ (it.key == Key .DirectionUp || (it.isShiftPressed && it.key == Key .Tab ))
250+ ) {
251+ focusRequester.requestFocus()
252+ return @onKeyEvent true
253+ }
254+ return @onKeyEvent false
255+ }
256+ } else {
257+ Modifier
258+ }
259+ ),
233260 onClick = {
234261 fabMenuExpanded = false
235262 onItemClick(item.second)
0 commit comments