diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java index f2e8f968e8af50..704d0956d85559 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java @@ -174,14 +174,16 @@ protected void onScrollChanged(int horiz, int vert, int oldHoriz, int oldVert) { @Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) { ReactContext reactContext = (ReactContext) getContext(); - ReactEditTextInputConnectionWrapper inputConnectionWrapper = - new ReactEditTextInputConnectionWrapper(super.onCreateInputConnection(outAttrs), reactContext, this); + InputConnection inputConnection = super.onCreateInputConnection(outAttrs); + if (inputConnection != null) { + inputConnection = new ReactEditTextInputConnectionWrapper(inputConnection, reactContext, this); + } if (isMultiline() && getBlurOnSubmit()) { // Remove IME_FLAG_NO_ENTER_ACTION to keep the original IME_OPTION outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION; } - return inputConnectionWrapper; + return inputConnection; } @Override diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditTextInputConnectionWrapper.java b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditTextInputConnectionWrapper.java index 5a06db595e2027..86d31ea54f5dab 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditTextInputConnectionWrapper.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditTextInputConnectionWrapper.java @@ -94,14 +94,15 @@ public boolean setComposingText(CharSequence text, int newCursorPosition) { int previousSelectionEnd = mEditText.getSelectionEnd(); String key; boolean consumed = super.setComposingText(text, newCursorPosition); + int currentSelectionStart = mEditText.getSelectionStart(); boolean noPreviousSelection = previousSelectionStart == previousSelectionEnd; - boolean cursorDidNotMove = mEditText.getSelectionStart() == previousSelectionStart; - boolean cursorMovedBackwards = mEditText.getSelectionStart() < previousSelectionStart; - if ((noPreviousSelection && cursorMovedBackwards) - || !noPreviousSelection && cursorDidNotMove) { + boolean cursorDidNotMove = currentSelectionStart == previousSelectionStart; + boolean cursorMovedBackwardsOrAtBeginningOfInput = + (currentSelectionStart < previousSelectionStart) || currentSelectionStart <= 0; + if (cursorMovedBackwardsOrAtBeginningOfInput || (!noPreviousSelection && cursorDidNotMove)) { key = BACKSPACE_KEY_VALUE; } else { - key = String.valueOf(mEditText.getText().charAt(mEditText.getSelectionStart() - 1)); + key = String.valueOf(mEditText.getText().charAt(currentSelectionStart - 1)); } dispatchKeyEventOrEnqueue(key); return consumed;