Skip to content

Let ref.focus() focus a View from a tap on Android - #60529

Open
dileepapeiris wants to merge 3 commits into
react:mainfrom
dileepapeiris:fix/android-view-focus-touch-mode
Open

dileepapeiris wants to merge 3 commits into
react:mainfrom
dileepapeiris:fix/android-view-focus-touch-mode

Conversation

@dileepapeiris

Copy link
Copy Markdown

Summary:

Fixes #60195.

With enableImperativeFocus on, ref.focus() on a View sends the focus view command, and ReactViewGroup.requestFocusFromJS() calls requestFocus(FOCUS_DOWN, null). When the call comes from a tap's onPress the window is in touch mode, where Android's View.requestFocus() refuses a view that is focusable but not focusable in touch mode. A Pressable or focusable View is exactly that (BaseViewManager resets focusableInTouchMode to false), so the request is dropped and nothing holds focus. The same onPress run from Enter on a hardware keyboard works, because a key event leaves touch mode. focus() on a TextInput works from a tap because an EditText is focusable in touch mode.

requestFocusFromJS() now marks the view focusable in touch mode for the duration of the request and restores the previous value afterwards, so the view's props keep their meaning and the next touch-mode transition behaves as before. A view that is not focusable is left alone, as before, since setFocusableInTouchMode(true) would also make it focusable.

Changelog:

[ANDROID] [FIXED] - ref.focus() on a View now focuses the view when called from a tap

Test Plan:

Reproducer: https://github.com/dileepapeiris/react-native-view-focus-touch-mode-repro (Android 17 emulator, react-native 0.88.1 with enableImperativeFocus forced on in MainApplication.kt). tools/run.sh taps the buttons with adb shell input tap and reads which view holds focus from uiautomator dump.

Step Before After
tap "1. focus() the view" nothing focused view target focused
tap "2. focus() the text input" text input target text input target
tap "3. focus() the view after 500 ms" unchanged view target focused
fresh launch, Tab, Enter on button 1 view target view target

After tapping "1. focus() the view", before on the left and after on the right:

Before: nothing focused. After: the view target is focused

Both flows (tap flow, then Tab, Enter and a tap on a fresh launch), before and after:

Before After

Recordings: before.mp4, after.mp4. Per-step screenshots and the focus log of each run are in the reproducer's media/before and media/after.

Unit tests: ./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest --tests 'com.facebook.react.views.view.ReactViewGroupTest' with two new cases, a focusable view attached to a window takes focus in touch mode and keeps isFocusableInTouchMode == false, and a non-focusable view stays non-focusable.

@github-actions github-actions Bot added the Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. label Dec 4, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Android: ref.focus() on a View does nothing when called from a tap (enableImperativeFocus), as the view is not focusable in touch mode

1 participant