bug(keyboard): insets animation callbacks can run after handleOnDestroy cleared the listener on Android

未关闭 适合新手
#2,647 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
android, java
领域
mobile-dev

调研方向

从 Android 键盘插件的回调 Keyboard.java、onStart 和 onEnd 入手,报告指出 listener 调用可能在 teardown 后发生。检查 KeyboardPlugin.handleOnDestroy() 如何清除 listener,然后为两个回调添加 listener 为 null 时的保护,同时保留现有的返回行为。完成标准是忽略 teardown 后到达的回调;issue 未指定测试文件。

由索引模型根据 Issue 内容生成。

描述

platform: android

Bug Report

Plugin(s)

@capacitor/keyboard 8.0.6 (same code in 9.0.0-next.3)

Capacitor Version
@capacitor/core: 8.6.0
@capacitor/android: 8.6.0
@capacitor/keyboard: 8.0.6
Platform(s)

Android

Current Behavior

When the activity is destroyed while an IME insets animation is running (for example a configuration change that relaunches the activity), InsetsController.cancelExistingAnimations dispatches onEnd to the plugin's WindowInsetsAnimationCompat.Callback during ViewRootImpl.dispatchDetachedFromWindow. By then KeyboardPlugin.handleOnDestroy() may already have called implementation.setKeyboardEventListener(null), and Keyboard's onStart/onEnd call keyboardEventListener.onKeyboardEvent(...) without a null check, so the callback can throw a NullPointerException during teardown.

On 8.0.3 the same teardown crashed one step earlier because ViewCompat.getRootWindowInsets(rootView) returned null:

java.lang.NullPointerException
  at androidx.core.view.WindowInsetsCompat.isVisible (WindowInsetsCompat.java:711)
  at com.capacitorjs.plugins.keyboard.Keyboard$1.onEnd (Keyboard.java:112)
  at androidx.core.view.WindowInsetsAnimationCompat$Impl30$ProxyCallback.onEnd (WindowInsetsAnimationCompat.java:1099)
  at android.view.InsetsController.cancelExistingAnimations (InsetsController.java:1915)
  at android.view.ViewRootImpl.dispatchDetachedFromWindow (ViewRootImpl.java:6231)
  at android.app.ActivityThread.handleDestroyActivity (ActivityThread.java:6047)
  at android.app.ActivityThread.handleRelaunchActivityInner (ActivityThread.java:6322)

8.0.6 added if (insets == null) return;, which covers that case, but the listener can still be null at that point.

Expected Behavior

Insets animation callbacks that arrive after handleOnDestroy are ignored.

Code Reproduction

Teardown race: open the keyboard in any Capacitor app and trigger an activity relaunch (for example change the system font size or dark mode with a configuration the app does not handle) while the IME animation runs. We saw the 8.0.3 variant in production once in 28 days.

Other Technical Details

The fix we ship as a patch, in both callbacks:

 WindowInsetsCompat insets = ViewCompat.getRootWindowInsets(rootView);
-if (insets == null) return;
+if (insets == null || keyboardEventListener == null) return;

(onStart returns super.onStart(animation, bounds) in the same condition.)

Additional Context

Happy to open a pull request against main with this change if you agree with the approach.

This report was prepared with AI assistance (Claude) on behalf of the app team.

主要语言
Java
星标
688
派生
690
平均合并
16 小时 15 分钟
30 天内合并 PR
7

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ionic-team/capacitor-plugins 的其他 Issue

查看 ionic-team/capacitor-plugins 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。