From 50922608411adac2572744d41cc8507adbf5ac77 Mon Sep 17 00:00:00 2001 From: Oke Hargens Date: Tue, 18 Nov 2025 23:44:48 +0100 Subject: [PATCH 1/2] fix(android): unregister window insets callback on destroy --- .../java/com/capacitorjs/plugins/keyboard/Keyboard.java | 7 +++++++ .../com/capacitorjs/plugins/keyboard/KeyboardPlugin.java | 5 ++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/com/capacitorjs/plugins/keyboard/Keyboard.java b/android/src/main/java/com/capacitorjs/plugins/keyboard/Keyboard.java index a132d26..f49b7ee 100644 --- a/android/src/main/java/com/capacitorjs/plugins/keyboard/Keyboard.java +++ b/android/src/main/java/com/capacitorjs/plugins/keyboard/Keyboard.java @@ -115,6 +115,13 @@ public void onEnd(@NonNull WindowInsetsAnimationCompat animation) { frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams(); } + public void destroy() { + if (rootView != null) { + ViewCompat.setWindowInsetsAnimationCallback(rootView, null); + rootView = null; + } + } + public void show() { ((InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(activity.getCurrentFocus(), 0); } diff --git a/android/src/main/java/com/capacitorjs/plugins/keyboard/KeyboardPlugin.java b/android/src/main/java/com/capacitorjs/plugins/keyboard/KeyboardPlugin.java index 722f94a..3a30a59 100644 --- a/android/src/main/java/com/capacitorjs/plugins/keyboard/KeyboardPlugin.java +++ b/android/src/main/java/com/capacitorjs/plugins/keyboard/KeyboardPlugin.java @@ -98,6 +98,9 @@ void onKeyboardEvent(String event, int size) { @Override protected void handleOnDestroy() { - implementation.setKeyboardEventListener(null); + if (implementation != null) { + implementation.destroy(); + implementation.setKeyboardEventListener(null); + } } } From 77b6dbaa64b0def33a6a6c2aea776852a5b0e853 Mon Sep 17 00:00:00 2001 From: Oke Hargens Date: Tue, 18 Nov 2025 23:39:31 +0100 Subject: [PATCH 2/2] fix(android): guard keyboard callbacks when insets missing --- .../plugins/keyboard/Keyboard.java | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/android/src/main/java/com/capacitorjs/plugins/keyboard/Keyboard.java b/android/src/main/java/com/capacitorjs/plugins/keyboard/Keyboard.java index f49b7ee..9bcb796 100644 --- a/android/src/main/java/com/capacitorjs/plugins/keyboard/Keyboard.java +++ b/android/src/main/java/com/capacitorjs/plugins/keyboard/Keyboard.java @@ -75,8 +75,12 @@ public WindowInsetsAnimationCompat.BoundsCompat onStart( @NonNull WindowInsetsAnimationCompat animation, @NonNull WindowInsetsAnimationCompat.BoundsCompat bounds ) { - boolean showingKeyboard = ViewCompat.getRootWindowInsets(rootView).isVisible(WindowInsetsCompat.Type.ime()); WindowInsetsCompat insets = ViewCompat.getRootWindowInsets(rootView); + if (insets == null) { + return super.onStart(animation, bounds); + } + + boolean showingKeyboard = insets.isVisible(WindowInsetsCompat.Type.ime()); int imeHeight = insets.getInsets(WindowInsetsCompat.Type.ime()).bottom; DisplayMetrics dm = activity.getResources().getDisplayMetrics(); final float density = dm.density; @@ -85,10 +89,12 @@ public WindowInsetsAnimationCompat.BoundsCompat onStart( possiblyResizeChildOfContent(showingKeyboard); } - if (showingKeyboard) { - keyboardEventListener.onKeyboardEvent(EVENT_KB_WILL_SHOW, Math.round(imeHeight / density)); - } else { - keyboardEventListener.onKeyboardEvent(EVENT_KB_WILL_HIDE, 0); + if (keyboardEventListener != null) { + if (showingKeyboard) { + keyboardEventListener.onKeyboardEvent(EVENT_KB_WILL_SHOW, Math.round(imeHeight / density)); + } else { + keyboardEventListener.onKeyboardEvent(EVENT_KB_WILL_HIDE, 0); + } } return super.onStart(animation, bounds); } @@ -96,16 +102,22 @@ public WindowInsetsAnimationCompat.BoundsCompat onStart( @Override public void onEnd(@NonNull WindowInsetsAnimationCompat animation) { super.onEnd(animation); - boolean showingKeyboard = ViewCompat.getRootWindowInsets(rootView).isVisible(WindowInsetsCompat.Type.ime()); WindowInsetsCompat insets = ViewCompat.getRootWindowInsets(rootView); + if (insets == null || keyboardEventListener == null) { + return; + } + + boolean showingKeyboard = insets.isVisible(WindowInsetsCompat.Type.ime()); int imeHeight = insets.getInsets(WindowInsetsCompat.Type.ime()).bottom; DisplayMetrics dm = activity.getResources().getDisplayMetrics(); final float density = dm.density; - if (showingKeyboard) { - keyboardEventListener.onKeyboardEvent(EVENT_KB_DID_SHOW, Math.round(imeHeight / density)); - } else { - keyboardEventListener.onKeyboardEvent(EVENT_KB_DID_HIDE, 0); + if (keyboardEventListener != null) { + if (showingKeyboard) { + keyboardEventListener.onKeyboardEvent(EVENT_KB_DID_SHOW, Math.round(imeHeight / density)); + } else { + keyboardEventListener.onKeyboardEvent(EVENT_KB_DID_HIDE, 0); + } } } }