Fix Keyboard events and KeyboardAvoidingView on Android (edge-to-edge) (#55855)

Summary:
`KeyboardAvoidingView` has several issues on Android 15+ and when `edgeToEdgeEnabled` is set to `true` on Android < 15:

1. **Keyboard events relied on a legacy detection path for Android < 30** (`checkForKeyboardEventsLegacy`), which used height heuristics (`heightDiff > mMinKeyboardHeightDetected`) to guess whether the keyboard was visible.
2. **`keyboardDidHide` reported incorrect `screenY` values.** It used `mVisibleViewArea.height()`, which in edge-to-edge mode doesn't correspond to the actual screen bottom.
3. **`KeyboardAvoidingView` used `_onKeyboardChange` for `keyboardDidHide` on Android**, which stores the event instead of nullifying it. After keyboard dismissal, any subsequent `onLayout` would re-enter `_updateBottomIfNecessary` with stale coordinates, causing a render loop (especially visible with `behavior="height"` in edge-to-edge mode, where `frame.y` shifts slightly on each layout cycle).

This PR:

- Removes `checkForKeyboardEventsLegacy` and uses `WindowInsetsCompat` APIs (via AndroidX)
- Fixes `keyboardDidHide` to report `screenY` as `mVisibleViewArea.bottom + barInsets.bottom`
- Fixes the Android `keyboardDidHide` listener in `KeyboardAvoidingView` to use `_onKeyboardHide`, matching the iOS behavior and breaking the render loop.

Closes https://github.com/facebook/react-native/issues/49759

## Changelog:

[ANDROID] [FIXED] - Fix `KeyboardAvoidingView` on Android 15+ / with `edgeToEdgeEnabled`

Pull Request resolved: https://github.com/facebook/react-native/pull/55855

Test Plan:
Tested with `rn-tester` KeyboardAvoidingView example on:
- Android 16 (API 36) (edge-to-edge is enforced)
- Android 14 (API 34) with `edgeToEdgeEnabled` set to `true` or `false`
- Android 7 (API 24) with `edgeToEdgeEnabled` set to `true` or `false`

For each, verified:
- `behavior="padding"`: keyboard open/close adjusts padding correctly
- `behavior="position"`: keyboard open/close translates content correctly
- `behavior="height"`: keyboard open/close works without render loop or glitching
- `keyboardDidShow` and `keyboardDidHide` events report correct coordinates

Reviewed By: mdvacca

Differential Revision: D100437445

Pulled By: alanleedev

fbshipit-source-id: 23b89a4380d02a0f524d587f89d023d21782406c
This commit is contained in:
Mathieu Acthernoene authored and meta-codesync[bot] committed 2026-04-17 01:25:17 -07:00
1 parent a8d44bcac2
commit 4a6c933cce
2 files changed
+11 -73

No files matched your search

@@ -208,7 +208,7 @@ class KeyboardAvoidingView extends React.Component<
];
} else {
this._subscriptions = [
Keyboard.addListener('keyboardDidHide', this._onKeyboardChange),
Keyboard.addListener('keyboardDidHide', this._onKeyboardHide),
Keyboard.addListener('keyboardDidShow', this._onKeyboardChange),
];
}
@@ -17,7 +17,6 @@ import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.BlendMode;
import android.graphics.Canvas;
import android.graphics.Insets;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
@@ -25,18 +24,18 @@ import android.os.Build;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.DisplayCutout;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowInsets;
import android.view.WindowManager;
import android.widget.FrameLayout;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.facebook.common.logging.FLog;
import com.facebook.infer.annotation.Assertions;
import com.facebook.infer.annotation.ThreadConfined;
@@ -801,11 +800,7 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
@VisibleForTesting
/* package */ void simulateCheckForKeyboardForTesting() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
getCustomGlobalLayoutListener().checkForKeyboardEvents();
} else {
getCustomGlobalLayoutListener().checkForKeyboardEventsLegacy();
}
getCustomGlobalLayoutListener().checkForKeyboardEvents();
}
private CustomGlobalLayoutListener getCustomGlobalLayoutListener() {
@@ -934,16 +929,13 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
private class CustomGlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {
private final Rect mVisibleViewArea;
private final int mMinKeyboardHeightDetected;
private boolean mKeyboardIsVisible = false;
private int mKeyboardHeight = 0; // Only used in checkForKeyboardEventsLegacy path
private int mDeviceRotation = 0;
/* package */ CustomGlobalLayoutListener() {
DisplayMetricsHolder.initDisplayMetricsIfNotInitialized(getContext().getApplicationContext());
mVisibleViewArea = new Rect();
mMinKeyboardHeightDetected = (int) PixelUtil.toPixelFromDIP(60);
}
@Override
@@ -952,31 +944,25 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
checkForKeyboardEvents();
} else {
checkForKeyboardEventsLegacy();
}
checkForKeyboardEvents();
checkForDeviceOrientationChanges();
checkForDeviceDimensionsChanges();
}
@RequiresApi(api = Build.VERSION_CODES.R)
private void checkForKeyboardEvents() {
getRootView().getWindowVisibleDisplayFrame(mVisibleViewArea);
WindowInsets rootInsets = getRootView().getRootWindowInsets();
WindowInsetsCompat rootInsets = ViewCompat.getRootWindowInsets(getRootView());
if (rootInsets == null) {
return;
}
boolean keyboardIsVisible = rootInsets.isVisible(WindowInsets.Type.ime());
boolean keyboardIsVisible = rootInsets.isVisible(WindowInsetsCompat.Type.ime());
if (keyboardIsVisible != mKeyboardIsVisible) {
mKeyboardIsVisible = keyboardIsVisible;
Insets barInsets = rootInsets.getInsets(WindowInsetsCompat.Type.systemBars());
if (keyboardIsVisible) {
Insets imeInsets = rootInsets.getInsets(WindowInsets.Type.ime());
Insets barInsets = rootInsets.getInsets(WindowInsets.Type.systemBars());
Insets imeInsets = rootInsets.getInsets(WindowInsetsCompat.Type.ime());
int height = imeInsets.bottom - barInsets.bottom;
ViewGroup.LayoutParams rootLayoutParams = getRootView().getLayoutParams();
@@ -999,7 +985,7 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
sendEvent(
"keyboardDidHide",
createKeyboardEventPayload(
PixelUtil.toDIPFromPixel(mVisibleViewArea.height()),
PixelUtil.toDIPFromPixel(mVisibleViewArea.bottom + barInsets.bottom),
0,
PixelUtil.toDIPFromPixel(mVisibleViewArea.width()),
0));
@@ -1007,54 +993,6 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
}
}
private void checkForKeyboardEventsLegacy() {
getRootView().getWindowVisibleDisplayFrame(mVisibleViewArea);
int notchHeight = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
WindowInsets insets = getRootView().getRootWindowInsets();
if (insets != null) {
DisplayCutout displayCutout = insets.getDisplayCutout();
if (displayCutout != null) {
notchHeight = displayCutout.getSafeInsetTop();
}
}
}
final int heightDiff =
DisplayMetricsHolder.getWindowDisplayMetrics().heightPixels
- mVisibleViewArea.bottom
+ notchHeight;
boolean isKeyboardShowingOrKeyboardHeightChanged =
mKeyboardHeight != heightDiff && heightDiff > mMinKeyboardHeightDetected;
if (isKeyboardShowingOrKeyboardHeightChanged) {
mKeyboardHeight = heightDiff;
mKeyboardIsVisible = true;
sendEvent(
"keyboardDidShow",
createKeyboardEventPayload(
PixelUtil.toDIPFromPixel(mVisibleViewArea.bottom),
PixelUtil.toDIPFromPixel(mVisibleViewArea.left),
PixelUtil.toDIPFromPixel(mVisibleViewArea.width()),
PixelUtil.toDIPFromPixel(mKeyboardHeight)));
return;
}
boolean isKeyboardHidden = mKeyboardHeight != 0 && heightDiff <= mMinKeyboardHeightDetected;
if (isKeyboardHidden) {
mKeyboardHeight = 0;
mKeyboardIsVisible = false;
sendEvent(
"keyboardDidHide",
createKeyboardEventPayload(
PixelUtil.toDIPFromPixel(mVisibleViewArea.height()),
0,
PixelUtil.toDIPFromPixel(mVisibleViewArea.width()),
0));
}
}
private void checkForDeviceOrientationChanges() {
final int rotation =
((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE))