Skip to content

[Android] Fix one-frame content jump in multiline TextInput when a line is added or removed - #60402

Closed
tomekzaw wants to merge 1 commit into
react:mainfrom
tomekzaw:fix/android-textinput-immediate-state-update
Closed

tomekzaw wants to merge 1 commit into
react:mainfrom
tomekzaw:fix/android-textinput-immediate-state-update

Conversation

@tomekzaw

Copy link
Copy Markdown
Contributor

Summary:

Fixes #46813.

On Android, a multiline TextInput re-flows and repaints its text on the keystroke, but its frame only changed once the state update sent from ReactTextInputTextWatcher was flushed asynchronously: next Choreographer tick, commit and layout on the JS thread, mount on the following UI frame. Every change that adds or removes a line therefore drew one frame with the new text and the old height, which shows up as a content jump (and the siblings below the input lag by a frame).

This dispatches that state update with UpdateMode.unstable_Immediate (the same mechanism iOS uses for scroll offsets behind enableImmediateUpdateModeForContentOffsetChanges), so the commit, layout and mount happen inside onTextChanged, before the frame is drawn. The cached spannable is refreshed before the text watchers run so the synchronous measure sees the new text. Unlike #52534 this does not run JS on the UI thread.

Changelog:

[ANDROID] [FIXED] - TextInput: fix one-frame content jump of multiline inputs when a line is added or removed

Test Plan:

RNTester Playground with the reproducer from #46813 (<TextInput multiline style={{fontSize: 20, width: 300, padding: 0}} /> plus a sibling below), Pixel emulator (API 36). A temporary log in ReactEditText.onDraw reported every frame drawn with a stale height: before, exactly one per height change; after, none. Controlled (value/onChangeText), uncontrolled and maxHeight-capped inputs behave as before.

Before After
Recording before after
Frame after 1st Enter before-1 after-1
Frame after 2nd Enter before-2 after-2
Frame after Backspace before-3 after-3

Full-resolution videos: before.mp4, after.mp4.

Benchmarks

Release RNTester (assembleRelease, arm64) with the reproducer above, A = main (5dc1d78), B = this PR, on four Argent Fleet phones, A/B/B/A order, 2 blocks × 2 runs per arm (frame table) and 1 block × 2 runs per arm (CPU table). Each run injects 10 × (20 characters + Enter) with adb shell input into the focused multiline input (210 keystrokes; each injected character fires two native text changes, so B commits ~440 times per run).

UI-thread time per input-handling frame (dumpsys gfxinfo framestats, SyncQueued − HandleInputStart, all input frames of all runs pooled; jank = frame total > 16.67 ms):

phone runs A/B input frames A/B UI-thread p50 A → B (ms) Δ p50 (95% CI) p90 A → B p99 A → B jank % A → B
Galaxy A16 (Samsung, Android 16) 8/8 1843/1842 3.53 → 2.58 -0.96 [-1.04, -0.90]* 4.70 → 3.51 7.05 → 4.84 4.3 → 3.5
Galaxy A57 5G (Samsung, Android 17) 8/8 1920/1899 2.14 → 1.54 -0.60 [-0.64, -0.56]* 2.96 → 2.33 4.48 → 3.32 0.5 → 1.6
moto g57 power (Motorola, Android 17) 8/8 1261/1530 2.08 → 1.76 -0.32 [-0.33, -0.30]* 2.38 → 2.01 3.34 → 2.49 0.2 → 0.5
Pixel 9a (Google, Android 17) 8/8 915/941 2.20 → 1.67 -0.53 [-0.61, -0.46]* 3.07 → 2.41 4.36 → 3.47 0.5 → 0.6

CPU time per keystroke (utime+stime of the main thread and mqt_v_js from /proc over the typing loop ÷ 210):

phone runs A/B main thread CPU ms per keystroke A → B Δ (95% CI) JS thread CPU ms per keystroke A → B Δ (95% CI) typing wall time ms A → B
Galaxy A16 (Samsung, Android 16) 4/4 9.39 → 10.87 +1.48 [+1.29, +1.70]* 4.95 → 3.40 -1.55 [-1.67, -1.42]* 9480 → 9654
Galaxy A57 5G (Samsung, Android 17) 4/4 13.89 → 15.00 +1.11 [+0.63, +1.61]* 4.98 → 3.14 -1.83 [-2.01, -1.68]* 9171 → 9072
moto g57 power (Motorola, Android 17) 4/4 7.56 → 9.64 +2.08 [+1.87, +2.27]* 2.19 → 1.87 -0.32 [-0.40, -0.24]* 6632 → 7052
Pixel 9a (Google, Android 17) 4/3 5.37 → 6.30 +0.93 [+0.82, +1.06]* 2.61 → 2.44 -0.16 [-0.36, +0.06] 7775 → 7789

* = bootstrap 95% CI excludes 0.

Reading: the commit + layout + mount of a native text change moves from the JS thread to the UI thread (atrace: 0.7–1.3 ms per ShadowTree::commit on the UI thread, 0.9–2.0 ms on the JS thread before), which is ~0.5–1 ms of extra main-thread CPU per text change. The work no longer lands in a separate frame, so the UI-thread time inside input frames and the p99 go down on all four phones; the extra time is spent in key-event handling before the frame. The JS thread is freed by the same amount.

yarn flow-check, yarn format-check-kotlin pass.

🤖 Generated with Claude Code

A multiline TextInput re-flows and repaints its text on the keystroke, but
its frame only changed once the state update sent from the text watcher
was flushed asynchronously (next Choreographer tick, commit and layout on
the JS thread, mount on the following UI frame). Every change that adds
or removes a line therefore drew one frame with the new text and the old
height, which showed up as a content jump.

Dispatch that state update with UpdateMode.unstable_Immediate so the
commit, layout and mount happen inside onTextChanged, before the frame
is drawn, and refresh the cached spannable before notifying the text
watchers so the synchronous measure sees the new text.

Fixes react#46813

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 30, 2026
@facebook-github-tools facebook-github-tools Bot added p: Software Mansion Partner: Software Mansion Partner labels Nov 30, 2026
@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 Nov 30, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Nov 30, 2026
@tomekzaw

Copy link
Copy Markdown
Contributor Author

Closing: this is already fixed in PR #59419 (same mechanism, limited to multiline line-count changes, behind fixMultilineContentJumpAndroid). Verified on current main with the reproducer from #46813: no stale-height frames with the flag enabled.

@tomekzaw tomekzaw closed this Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. p: Software Mansion Partner: Software Mansion Partner Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Android] Content jump in multiline TextInput when adding/removing line of text due to delayed layout update happening after content update

1 participant