Skip to content

fix(android): update keyboard state on keyboardDidShow - #1698

Open
Nezz wants to merge 1 commit into
kirillzyusko:mainfrom
Nezz:fix/keyboard-state-did-show
Open

Nezz wants to merge 1 commit into
kirillzyusko:mainfrom
Nezz:fix/keyboard-state-did-show

Conversation

@Nezz

@Nezz Nezz commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

📜 Description

Open the keyboard on an Android phone, rotate it, and the library loses track of the keyboard: it's still on screen, but useKeyboardState and KeyboardController insist it's closed. With this PR the keyboard state also updates on keyboardDidShow, so it stays in sync no matter how the keyboard got there.

Reproduction: https://github.com/Nezz/expo-repro/tree/e299109d8b565d6d2dc6f71fdb5041429ac0bdbd (Expo SDK 57.0.24, React Native 0.86.3 with the New Architecture, react-native-keyboard-controller 1.22.8, Android).

bun install
npx expo run:android
  1. Tap the text field. The keyboard opens and the screen shows useKeyboardState: isVisible true, height 336.
  2. Rotate to landscape
  3. Rotate back to portrait

Expected: the keyboard is reported open, height is matches the virtual keyboard's height.

Actual: keyboard is reported closed, height is reported 0

💡 Motivation and Context

When an Android phone rotates, the keyboard doesn't animate to its new position — it jumps. In that case the library skips the usual event sequence and quietly re-syncs the keyboard position, emitting only keyboardDidHide and keyboardDidShow, never keyboardWillShow:

📢 Changelog

JS

  • Keep the keyboard state correct on Android rotation: useKeyboardState, KeyboardController.isVisible() and KeyboardController.state() now update on keyboardDidShow too

📸 Screenshots (if appropriate):

Before (1.22.8), Pixel 7 emulator, Android 16 After, Pixel 7 emulator, Android 16

Before: the keyboard is open, but useKeyboardState shows isVisible false, height 0 which is incorrect

📝 Checklist

  • CI successfully passed
  • I added new mocks and corresponding unit-tests if library API was changed

@github-actions

Copy link
Copy Markdown
Contributor

📊 Package size report

Current size Target Size Difference
348211 bytes 347927 bytes 284 bytes 📈

@Nezz
Nezz marked this pull request as ready for review October 21, 2026 12:47
@github-actions

Copy link
Copy Markdown
Contributor

LGTM

@Elehiggle

Copy link
Copy Markdown

Thanks for this fix. I ran the pinned repro with and without it on two emulators: 10 portrait → landscape → portrait round trips per device and build, each from a fresh launch with the field focused, landscape held for 1 to 3 s. Release build; bun install from the repro's lockfile gave Expo 57.0.30, React Native 0.86.3 and react-native-keyboard-controller 1.22.8. "With this PR" means that 1.22.8 package with the PR's changes to src/module.ts and src/hooks/useKeyboardState/index.ts at d186d29 applied by hand, plus the same edits in its built lib/ files. It is not a build of the PR branch.

Device Before (1.22.8): stale With this PR: stale
Pixel 9 Pro emulator (Android 16/agent) 9/10 0/10
Pixel Fold emulator, folded (Android 16/agent) 0/10 0/10

Stale means that about 5 s after the round trip the screen showed useKeyboardState: isVisible false, height 0 and KeyboardController.isVisible(): false, while dumpsys input_method reported mInputShown=true. In every stale run, isVisible was still false at about 10 s.

On the Pixel 9 Pro, 9 runs in each set ended with keyboardDidHide and then keyboardDidShow without keyboardWillShow. Before the PR all 9 went stale; with it none did. The one run per set where the keyboard came back with keyboardWillShow was correct in both. On the folded Pixel Fold the keyboard came back with keyboardWillShow in all 20 runs, so the bug did not appear there, and the PR made no difference.

Load: the host (macOS 27.0.1, Apple silicon) was also running other work. Each run waited until the 1-minute load average was under 10. It was 5.1 to 9.9 when runs started and at most 12.2 when they ended.

The stale state also affects KeyboardController.dismiss(). I checked this on the Pixel 9 Pro emulator with a local copy of the repro screen that adds two buttons; that copy is not part of the public repro. Each button got its own fresh launch and was tapped at least 5 s after the round trip:

  • Before: the state was stale in 6 of 6 launches. dismiss({ keepFocus: true }) resolved after 0 ms, and the keyboard was still open 3 s later (3/3). dismiss() closed the keyboard, but it also resolved after 0 ms, with no keyboardDidHide before that (3/3).
  • With this PR: both closed the keyboard and resolved after keyboardDidHide, 335 to 353 ms after the call (3/3 each).

Emulator image on both devices: Google Play, arm64-v8a, API 36.1, build BE4B.251210.005; Gboard 18.4.1; Android Emulator 36.6.11.

gitcafe-production Bot pushed a commit to pingdotgg/t3code that referenced this pull request Oct 27, 2026
…extra render

Mirror kirillzyusko/react-native-keyboard-controller#1698: add a separate
keyboardDidShow listener instead of rewriting the keyboardWillShow one, so
the patch hunk can be dropped when that PR ships. Split the terminal's
object selector into primitives so the added didShow update bails out
instead of re-rendering the terminal screen on every keyboard show.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants