Skip to content

Commit a722ad6

Browse files
authored
cherry-pick(4.3-stable): prevent useAnimatedKeyboard "Can't change insets on a cancelled animation" crash (#9670) (#9868)
## Summary Cherry-picking for Reanimated 4.3.2 release - #9670 4.3 still has the pre-Kotlin `KeyboardAnimationCallback.java`, so this is a Java port of the same fix.
1 parent dfe4229 commit a722ad6

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

packages/react-native-reanimated/android/src/main/java/com/swmansion/reanimated/keyboard/KeyboardAnimationCallback.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class KeyboardAnimationCallback extends WindowInsetsAnimationCompat.Callb
1010
private final NotifyAboutKeyboardChangeFunction mNotifyAboutKeyboardChange;
1111
private static final int CONTENT_TYPE_MASK = WindowInsetsCompat.Type.ime();
1212
private final boolean mIsNavigationBarTranslucent;
13+
private boolean mPendingStartDispatch = false;
1314

1415
public KeyboardAnimationCallback(
1516
Keyboard keyboard,
@@ -30,7 +31,9 @@ public WindowInsetsAnimationCompat.BoundsCompat onStart(
3031
return bounds;
3132
}
3233
mKeyboard.onAnimationStart();
33-
mNotifyAboutKeyboardChange.call();
34+
// Dispatching synchronously here can cancel the still-pending insets
35+
// animation, so the dispatch is deferred to onProgress or onEnd.
36+
mPendingStartDispatch = true;
3437
return super.onStart(animation, bounds);
3538
}
3639

@@ -47,6 +50,7 @@ public WindowInsetsCompat onProgress(
4750
}
4851
}
4952
if (isAnyKeyboardAnimationRunning) {
53+
mPendingStartDispatch = false;
5054
mKeyboard.updateHeight(insets, mIsNavigationBarTranslucent);
5155
mNotifyAboutKeyboardChange.call();
5256
}
@@ -56,6 +60,10 @@ public WindowInsetsCompat onProgress(
5660
@Override
5761
public void onEnd(@NonNull WindowInsetsAnimationCompat animation) {
5862
if (isKeyboardAnimation(animation)) {
63+
if (mPendingStartDispatch) {
64+
mNotifyAboutKeyboardChange.call();
65+
}
66+
mPendingStartDispatch = false;
5967
mKeyboard.onAnimationEnd();
6068
mNotifyAboutKeyboardChange.call();
6169
}

0 commit comments

Comments
 (0)