Repository navigation
Android: Report FlutterView as a text editor while a text input client is attached - #193074
Conversation
There was a problem hiding this comment.
Code Review
This pull request implements onCheckIsTextEditor in FlutterView and isTextEditor in TextInputPlugin to report whether the view is currently acting as a text editor, with corresponding unit tests added. Feedback suggests adding a null check for textInputPlugin in onCheckIsTextEditor to prevent a potential NullPointerException during lifecycle transitions.
| public boolean onCheckIsTextEditor() { | ||
| return isAttachedToFlutterEngine() && textInputPlugin.isTextEditor(); | ||
| } |
There was a problem hiding this comment.
During attachToFlutterEngine and detachFromFlutterEngine, there are windows of time where isAttachedToFlutterEngine() returns true (because flutterEngine is non-null) but textInputPlugin is null (since they are not set/cleared atomically). If the Android framework triggers a focus or layout pass during these lifecycle transitions, onCheckIsTextEditor() can be called and will throw a NullPointerException.
To prevent this, we should explicitly check that textInputPlugin is non-null before calling isTextEditor().
| public boolean onCheckIsTextEditor() { | |
| return isAttachedToFlutterEngine() && textInputPlugin.isTextEditor(); | |
| } | |
| public boolean onCheckIsTextEditor() { | |
| return isAttachedToFlutterEngine() && textInputPlugin != null && textInputPlugin.isTextEditor(); | |
| } |
References
- Enforce defensive programming by ensuring appropriate null checks exist before object property accesses in languages that support nullable references.
There was a problem hiding this comment.
I think we should add this null check!
There was a problem hiding this comment.
The null window doesn't exist on detach: textInputPlugin is destroyed but never nulled, and isAttachedToFlutterEngine() goes false at renderSurface.detachFromRenderer(). On first attach there's a window between renderSurface.attachToRenderer() and the plugin assignment. But onCheckIsTextEditor() is only called from IMM's getStartInputFlags, from startInputInner/window-focus handling. Those run on the UI thread, and view focus changes are posted via dispatchCheckFocus, so they can't interleave with attachToFlutterEngine. Even if something re-entered there, startInputInner calls onCreateInputConnection right after, and that method has the same isAttachedToFlutterEngine() guard with an unguarded textInputPlugin dereference. I mirrored that guard deliberately. Happy to add the check anyway if you'd prefer it as defense.
There was a problem hiding this comment.
Thanks for the detailed explanation! For what it's worth, I think you're right, but can we add it just as an extra line of defense? Thanks!
camsim99
left a comment
There was a problem hiding this comment.
Other than resolving the Gemini comment, LGTM!
| public boolean onCheckIsTextEditor() { | ||
| return isAttachedToFlutterEngine() && textInputPlugin.isTextEditor(); | ||
| } |
There was a problem hiding this comment.
I think we should add this null check!
camsim99
left a comment
There was a problem hiding this comment.
LGTM! Thanks for the quick fix :) we'll need a second review.
Renzo-Olivares
left a comment
There was a problem hiding this comment.
LGTM! The other PR also targeted #159670, this uses the same approach so we should check if this also solves that issue so we can close it.
f281ea3 to
e5d8796
Compare
aad138f to
7772765
Compare
|
autosubmit label was removed for flutter/flutter/193074, because - The status or check suite Dashboard Checks has failed. Please fix the issues identified (or deflake) before re-applying this label. |
|
autosubmit label was removed for flutter/flutter/193074, because - The status or check suite Presubmit has failed. Please fix the issues identified (or deflake) before re-applying this label. |
|
auto label is removed for flutter/flutter/193074, Failed to enqueue flutter/flutter/193074 with HTTP 400: Pull request Required status check "Check Code Freeze" is expected.. |
|
auto label is removed for flutter/flutter/193074, Failed to enqueue flutter/flutter/193074 with HTTP 400: Pull request Required status check "Check Code Freeze" is expected.. |
|
auto label is removed for flutter/flutter/193074, Failed to enqueue flutter/flutter/193074 with HTTP 400: Pull request Required status check "Check Code Freeze" is expected.. |
|
auto label is removed for flutter/flutter/193074, Failed to enqueue flutter/flutter/193074 with HTTP 400: Pull request Required status check "Check Code Freeze" is expected.. |
flutter/flutter@2f09f96...06f32f8 2026-11-18 [email protected] Move macOS desktop builds to ThinLTO (flutter/flutter#196804) 2026-11-18 [email protected] Roll Dart SDK from 5ed55b4b2ebc to 753488ede427 (2 revisions) (flutter/flutter#196893) 2026-11-18 [email protected] Roll Packages from 9af69be to dca2e58 (7 revisions) (flutter/flutter#196892) 2026-11-18 [email protected] Roll Skia from 503a380ebecc to 81ee7327e81c (29 revisions) (flutter/flutter#196889) 2026-11-18 [email protected] Roll Dart SDK from 26e38955d9d4 to 5ed55b4b2ebc (4 revisions) (flutter/flutter#196843) 2026-11-18 [email protected] Fix permissions for action updates (flutter/flutter#196791) 2026-11-18 [email protected] iOS: Apply platform view mutators from FlutterPlatformViewLayer (flutter/flutter#196753) 2026-11-18 [email protected] [flutter_tools] Remove ambient Doctor, FeatureFlags, and device getters (flutter/flutter#196572) 2026-11-18 [email protected] Fix RelativeRect.lerp crash when b is null (flutter/flutter#196655) 2026-11-18 [email protected] [web] Harden WebP and GIF header readers against truncated and malformed buffers (flutter/flutter#196075) 2026-11-17 [email protected] Enable apps to control focus behavior during app lifecycle changes via `FocusLifecyclePolicy` (flutter/flutter#190570) 2026-11-17 [email protected] Don't re-run the code freeze check on label changes (flutter/flutter#196745) 2026-11-17 [email protected] Android: Report FlutterView as a text editor while a text input client is attached (flutter/flutter#193074) 2026-11-17 [email protected] Roll Packages from 4c6a7d3 to 9af69be (5 revisions) (flutter/flutter#196812) 2026-11-17 [email protected] [flutter_tools] Handle target VM filesystem errors in DevFS.create (flutter/flutter#196234) 2026-11-17 [email protected] [flutter_tools] Clarify analytics reporting status when on a feature branch (flutter/flutter#194496) 2026-11-17 [email protected] [web] Recover CanvasKit surfaces from WebGL failures without per-frame null crashes (flutter/flutter#196257) 2026-11-17 [email protected] [devicelab] Trigger navigation tap via Flutter Driver in service_extensions_test (flutter/flutter#194456) If this roll has caused a breakage, revert this CL and set the roller to dry run mode using the controls here: https://autoroll.skia.org/r/flutter-packages Please CC [email protected],[email protected] on the revert to ensure that a human is aware of the problem. To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose To report a problem with the AutoRoller itself, please file a bug: https://issues.skia.org/issues/new?component=1389291&template=1850622 Documentation for the AutoRoller is here: https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
FlutterViewnever overridesView.onCheckIsTextEditor(), so it always reportsfalse. Android'sInputMethodManagerturns that into theIS_TEXT_EDITORstart-input flag (InputMethodManager.java#L2945), and devices that shipconfig_preventImeStartupUnlessTextEditor=true(defaultfalseinconfig.xml#L3198; set by the low-RAM Android TV overlay on Chromecast with Google TV, Philips/TCL Google TVs) answer everystartInputwithout that flag withNO_EDITORand unbind the IME, unless a show is already pending for the window (InputMethodManagerService.startInputUncheckedLocked,shouldPreventImeStartupLocked).On those devices the sequence for a Flutter
TextFieldis:startInputreturnsNO_EDITORand kills the IME service: window focus gain, therestartInputinTextInputPlugin.setTextInputEditingState, and therestartInputin the IME hide listener. The client records no bind sequence (mBindSequence=-1).showTextInput→showSoftInputmarks a show as requested, so IMMS binds the IME again (logcat showsGoogleInputMethodService.onCreate()~30 ms after the show) and creates a session.onBindMethodwith the new sequence, which the client discards because its own sequence is-1:mInputViewStarted=true) but the app holds no session (mCurMethod=null, IMEmInputBinding=null).ViewRootImpl'sImeInputStagereturnsDISPATCH_NOT_HANDLEDfor every key, so D-pad events fall through to the view hierarchy, Flutter moves focus, the field detaches, and the keyboard hides. That is the "keyboard disappears on any button" from the issue.This also explains both workarounds reported on the issue: a platform-view
EditTextworks becauseEditText.onCheckIsTextEditor()istrue, and a hot restart heals it (until Back closes the keyboard) because astartInputissued while a show is pending bypassesshouldPreventImeStartupLockedand records a real sequence.dumpsys input_methodon a Chromecast 4K (Android 14, Gboard TV 17.3.09) while the keyboard was visible but dead:Every
StartInputhistory entry has reasonSESSION_CREATED_BY_IMEwith a freshimeToken. On a Google TV Streamer (4 GB, flag off, same Gboard build) the same flow showsmBindSequence == mCurSeq,mCurMethodpopulated, and reasonsWINDOW_FOCUS_GAIN/APP_CALLED_RESTART_INPUT_APIwith a stable token.Change
FlutterView.onCheckIsTextEditor()now returnsisAttachedToFlutterEngine() && textInputPlugin.isTextEditor(), mirroring the guard inonCreateInputConnection. The newTextInputPlugin.isTextEditor()istruewhen a framework client with an input type other thanTextInputType.noneis attached (the same conditionshowTextInputalready uses to decide whether to show the keyboard), or when a virtual-display platform view is the input target (itsInputConnectionis proxied throughFlutterView). Hybrid-composition platform views own theirInputConnection, so they stayfalse.Ordering is already right on the framework side:
EditableText._openInputConnectioncallsTextInput.attach(setClient) beforesetEditingStateandshow, so the input target isFRAMEWORK_CLIENTby the time the flag is read.clearTextInputClientresets the target, so the flag drops when the field detaches, which matchesEditTextbeing a text editor for as long as it exists.This is the union of the two earlier attempts at this override: #189662 (
FRAMEWORK_CLIENT || VIRTUAL_DISPLAY_PLATFORM_VIEW) and #187778 (FRAMEWORK_CLIENTwith a non-noneinput type), which @Renzo-Olivares suggested combining in #187778 (comment). Both were closed without landing; this PR only carries theonCheckIsTextEditorpart, not the focus-restore logic from #187778.Behavior to call out
IS_TEXT_EDITOR(hasEditorFocused()inImeVisibilityStateComputer.computeState) feeds three IMMS decisions, and this change flips all of them for aFlutterViewwith an attached client:SOFT_INPUT_STATE_UNSPECIFIED+ window focus gain: today IMMS hides the IME (HIDE_UNSPECIFIED_WINDOW) because the focused view is not an editor. With this change, regaining window focus while aTextFieldis attached no longer hides the keyboard, same as a nativeEditText. This likely fixes a class of "keyboard closes when a system dialog/popup is dismissed" reports; #187778 measured this override alone as the difference for keyboard restore after Home → Recents on a Pixel 7.SOFT_INPUT_STATE_UNSPECIFIED+adjustResize(or a large layout) +SOFT_INPUT_IS_FORWARD_NAVIGATION+ text editor: IMMS auto-shows the IME on window focus gain (SHOW_AUTO_EDITOR_FORWARD_NAV).EditTextapps get this today; Flutter apps with an attached client start getting it too. Flutter's app template setswindowSoftInputMode="adjustResize", so this applies to most apps. The flag is computed by the framework fromonCheckIsTextEditor()regardless of start reason, so it cannot be suppressed forWINDOW_GAINED_FOCUSonly; flagging it here for reviewers to weigh.Not affected: key routing (
FlutterView.dispatchKeyEventis unchanged),InputConnectionAdaptor, IME insets sync, and autofill (notifyViewEnteredalready usesmView).Verification
The affected Robolectric classes were run locally against this branch with the
shell/platform/android/test_runnerGradle configuration (compileSdk 36, Robolectric 4.16, Mockito 5.17.0, embedding dependencies fromtools/androidx/files.json) without a full engine build:io.flutter.embedding.android.FlutterViewTestio.flutter.plugin.editing.TextInputPluginTestFlutterViewTest.onCheckIsTextEditor_reflectsAttachedTextInputClientfails at the assertion aftersetClientwith theFlutterViewoverride removed and passes with it. The Java files are formatted with google-java-format 1.7, the version pinned inDEPS.The same mechanism is what Plezy ships today as an app-side workaround on Android TV: a 1×1 focusable child of
FlutterViewthat reportsonCheckIsTextEditor()from a Dart-side flag, delegatesonCreateInputConnectiontoFlutterView, and returnstruefromcheckInputConnectionProxy(flutterView); D-pad input inTextFields works on the affected Chromecast with that in place. This engine change makes the proxy view unnecessary.Fixes #177360
Pre-launch Checklist
///).If you need help, consider asking for advice on the #hackers-new channel on Discord.
If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance.
Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the
gemini-code-assistbot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.