[Android][Keyboard] More consistent inequality check to compute keyboard state #5874
+3
−3
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.
Summary
I have found the
useAnimatedKeyboard
hook to be particularly useful. Beyond the height value, I often use the state value too, which allows me to show or hide elements based on the keyboard's state. In this example, I combine it with a regular React NativeKeyboardAvoidingView
to display a toolbar above my keyboard.The issue I encountered is that the computed Android keyboard state becomes invalid after opening and then closing the keyboard for the first time. After some investigation, it appears that the keyboard gets a negative height upon closing, which disrupts some logic inside
keyboard.java
:As a result, the keyboard state cycle appears to be
0 (UNKNOWN) > 1 (OPENING) > 2 (OPEN) > 3 (CLOSING) > 2 (OPEN)
instead of the expected0 (UNKNOWN) > 1 (OPENING) > 2 (OPEN) > 3 (CLOSING) > 4 (CLOSED)
.There may be an issue with the fact that the height gets a negative value, but this PR does not intend to address it. Instead, I would like to make the code more robust regarding keyboard state computation by using an inequality check, specifically
mHeight <= 0
, instead of a strict equality checkmHeight == 0
.Test plan
I have added a ready-to-play repository.
It essentially implements the code described above. I've added some logs to illustrate that the keyboard state is incorrect on Android when opening and closing the keyboard. You can also observe that the keyboard gets a negative height after closing.