Repository navigation
Description
Description
On Android, with the enableImperativeFocus feature flag on, ref.focus() on a View does nothing when it is called from a tap's onPress: the view does not take focus, and no view holds focus afterwards. The same onPress, run by Enter on a hardware keyboard, focuses the view.
focus() sends the focus command (ReactNativeElement.js L170-L176), which ReactViewGroup.requestFocusFromJS turns into requestFocus(FOCUS_DOWN, null) (ReactViewGroup.kt L510-L516). After a tap the window is in touch mode, and Android's View.requestFocus(int, Rect) does not focus a view that "is focusable and it is not focusable in touch mode (isFocusableInTouchMode) while the device is in touch mode". A focusable View or Pressable is focusable but not focusable in touch mode, and requestFocusFromJS does not change that.
Steps to reproduce
The reproducer turns on enableImperativeFocus in MainApplication.kt and renders a Pressable "move focus", whose onPress calls focus() on the next Pressable, "focus target", and a text counting the presses.
- Build and install the release APK:
cd ReproducerApp && yarn install && cd android && ./gradlew assembleRelease && adb install -r app/build/outputs/apk/release/app-release.apk. - Open the app and tap "move focus". The press count goes up; "focus target" does not take focus.
- Restart the app, press Tab on a hardware keyboard to focus "move focus", and press Enter. The press count goes up and "focus target" takes focus.
Or run ./reproduce.sh with one Android device attached: it builds and installs the app, taps "move focus" through adb shell input tap on a fresh launch and reads which view holds focus from uiautomator dump, then on a fresh launch presses Tab and Enter and reads it again. It exits 1 while the tap leaves "focus target" without focus although Enter focuses it.
Expected: focus() focuses "focus target" whether the press came from a tap or from a key, as HTMLElement.focus() does on the web.
Seen: after the tap, no view holds focus; after Enter, "focus target" does.
React Native Version
0.90.0-nightly-20261120-bbfd39149
Affected Platforms
Runtime - Android
Output of npx @react-native-community/cli info
System:
OS: Windows 11 10.0.26300
CPU: "(32) x64 AMD Ryzen 9 9950X3D 16-Core Processor "
Memory: 127.39 GB / 255.67 GB
Binaries:
Node:
version: 26.12.1
path: C:\Program Files\nodejs\node.EXE
Yarn:
version: 1.22.22
path: C:\Users\<user>\AppData\Roaming\npm\yarn.CMD
npm:
version: 12.2.1
path: C:\Program Files\nodejs\npm.CMD
Watchman: Not Found
SDKs:
Android SDK: Not Found
Windows SDK:
AllowDevelopmentWithoutDevLicense: Enabled
Versions:
- 10.0.19041.0
- 10.0.26100.0
IDEs:
Android Studio: AI-262.9437.185.2621.16467767
Visual Studio: Not Found
Languages:
Java:
version: 17.0.20.1
path: C:\Program Files\Microsoft\jdk-17.0.20.101-hotspot\bin\javac.EXE
Ruby: Not Found
npmPackages:
"@react-native-community/cli":
installed: 21.0.0-alpha.1
wanted: 21.0.0-alpha.1
react:
installed: 19.3.0
wanted: 19.3.0
react-native:
installed: 0.90.0-nightly-20261120-bbfd39149
wanted: 0.90.0-nightly-20261120-bbfd39149
react-native-windows: Not Found
npmGlobalPackages:
"*react-native*": Not Found
Android:
hermesEnabled: true
newArchEnabled: true
iOS:
hermesEnabled: Not found
newArchEnabled: Not found
Stacktrace or Logs
./reproduce.sh at the reproducer's 6e53775, on an Android 15 emulator; no error is logged:
device: sdk_gphone64_x86_64, Android 15 (API 35), x86_64
react-native 0.90.0-nightly-20261120-bbfd39149, enableImperativeFocus on (MainApplication.kt)
== ./gradlew :app:assembleRelease -PreactNativeArchitectures=x86_64
== tap "move focus" on a fresh launch
after the tap and its onPress: "(nothing focused)" holds focus
== Tab to "move focus" on a fresh launch, then Enter
Tab 1: "move focus" holds focus
after Enter and its onPress: "focus target" holds focus
SUMMARY: ref.focus() from a tap's onPress leaves no view focused; from Enter's it focuses "focus target"
MANDATORY Reproducer
https://github.com/YevheniiKotyrlo/rn-view-ref-focus-touch-mode
Screenshots and Videos
No response
Activity