fix(android): reset WebView height when keyboard hides without animation#60
Open
hiebj wants to merge 2 commits intoionic-team:mainfrom
Open
fix(android): reset WebView height when keyboard hides without animation#60hiebj wants to merge 2 commits intoionic-team:mainfrom
hiebj wants to merge 2 commits intoionic-team:mainfrom
Conversation
The setOnApplyWindowInsetsListener only called possiblyResizeChildOfContent(true) when the keyboard was showing, but never called possiblyResizeChildOfContent(false) to reset. This meant the WebView height reset relied entirely on WindowInsetsAnimationCompat.Callback.onStart(), which requires an animation. When switching from an app with the keyboard open back to a Capacitor app, Android dismisses the keyboard without an animation. The insets listener detected the keyboard as visible (briefly, from the previous app's IME state) and shrunk the WebView, but the subsequent keyboard dismissal never triggered a reset because no animation fired. This fix passes the actual showingKeyboard state to possiblyResizeChildOfContent() so the insets listener handles both show and hide, matching the behavior already present in the animation callback's onStart(). Fixes ionic-team#30
ViewCompat.getRootWindowInsets() can return null when the root view is being detached (e.g. during activity destruction). All three call sites in the insets listener and animation callbacks now null-check before calling isVisible(), preventing a NullPointerException. Fixes ionic-team#51
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Two fixes for
Keyboard.java:1. Reset WebView height when keyboard hides without animation (commit 1)
The
setOnApplyWindowInsetsListeneronly callspossiblyResizeChildOfContent(true)when the keyboard is showing, but never callspossiblyResizeChildOfContent(false)to reset. This means the WebView height reset relies entirely onWindowInsetsAnimationCompat.Callback.onStart(), which requires an animation to fire.This fix passes the actual
showingKeyboardstate topossiblyResizeChildOfContent()so the insets listener handles both show and hide, matching the behavior already present inonStart().The change is safe because
possiblyResizeChildOfContentis idempotent — it checksusableHeightPrevious != usableHeightNowbefore doing any work, so redundant calls during normal keyboard animations are no-ops.2. Guard against null WindowInsets during teardown (commit 2)
ViewCompat.getRootWindowInsets()can returnnullwhen the root view is being detached (e.g. during activity destruction). All three call sites in the insets listener and animation callbacks now null-check before callingisVisible(), preventing aNullPointerException.Context
Fixes #30, fixes #51
When switching from an app with the keyboard open (e.g. Messages) back to a Capacitor app, the WebView remains resized as if the keyboard were open. The keyboard area appears greyed out even though no keyboard is visible.
Root cause: During app switching, Android dismisses the keyboard without an animation. The
setOnApplyWindowInsetsListenerfires and detectsshowingKeyboard = true(briefly, from the previous app's IME state), callingpossiblyResizeChildOfContent(true)to shrink the WebView. The keyboard then closes instantly (no focused input in the destination app), but because there's no animation,WindowInsetsAnimationCompat.Callback.onStart()never fires, andpossiblyResizeChildOfContent(false)is never called to reset the height.This was introduced in #40, which added the
setOnApplyWindowInsetsListenerbut only handled the show path.Also related to #8 (same symptom reported on iOS with swipe gestures).
Type of changes
Platforms affected
Tests
Tested on Android emulator (Pixel 9, API 35):
resizeOnFullScreen: trueAlso verified normal keyboard behavior:
Repro app from #30: https://github.com/irshadshalu/capacitor-keyboard-bug-sample