diff --git a/shell/platform/android/io/flutter/plugin/editing/InputConnectionAdaptor.java b/shell/platform/android/io/flutter/plugin/editing/InputConnectionAdaptor.java index ab4ed5c6b1d82..d702e2b7855ea 100644 --- a/shell/platform/android/io/flutter/plugin/editing/InputConnectionAdaptor.java +++ b/shell/platform/android/io/flutter/plugin/editing/InputConnectionAdaptor.java @@ -402,15 +402,16 @@ public boolean sendKeyEvent(KeyEvent event) { } else { // Enter a character. int character = event.getUnicodeChar(); - if (character != 0) { - int selStart = Math.max(0, Selection.getSelectionStart(mEditable)); - int selEnd = Math.max(0, Selection.getSelectionEnd(mEditable)); - int selMin = Math.min(selStart, selEnd); - int selMax = Math.max(selStart, selEnd); - if (selMin != selMax) mEditable.delete(selMin, selMax); - mEditable.insert(selMin, String.valueOf((char) character)); - setSelection(selMin + 1, selMin + 1); + if (character == 0) { + return false; } + int selStart = Math.max(0, Selection.getSelectionStart(mEditable)); + int selEnd = Math.max(0, Selection.getSelectionEnd(mEditable)); + int selMin = Math.min(selStart, selEnd); + int selMax = Math.max(selStart, selEnd); + if (selMin != selMax) mEditable.delete(selMin, selMax); + mEditable.insert(selMin, String.valueOf((char) character)); + setSelection(selMin + 1, selMin + 1); return true; } } diff --git a/shell/platform/android/test/io/flutter/plugin/editing/InputConnectionAdaptorTest.java b/shell/platform/android/test/io/flutter/plugin/editing/InputConnectionAdaptorTest.java index 458e75b7cfb02..2052a47934c0d 100644 --- a/shell/platform/android/test/io/flutter/plugin/editing/InputConnectionAdaptorTest.java +++ b/shell/platform/android/test/io/flutter/plugin/editing/InputConnectionAdaptorTest.java @@ -1106,6 +1106,17 @@ public void testSendKeyEvent_delKeyDeletesBackwardComplexEmojis() { assertEquals(Selection.getSelectionStart(editable), 0); } + @Test + public void testDoesNotConsumeBackButton() { + Editable editable = sampleEditable(0, 0); + InputConnectionAdaptor adaptor = sampleInputConnectionAdaptor(editable); + + FakeKeyEvent keyEvent = new FakeKeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK); + boolean didConsume = adaptor.sendKeyEvent(keyEvent); + + assertFalse(didConsume); + } + private static final String SAMPLE_TEXT = "Lorem ipsum dolor sit amet," + "\nconsectetur adipiscing elit."; diff --git a/shell/platform/android/test/io/flutter/util/FakeKeyEvent.java b/shell/platform/android/test/io/flutter/util/FakeKeyEvent.java index 2c9b9ceb1c3b8..75ce7b6ddb302 100644 --- a/shell/platform/android/test/io/flutter/util/FakeKeyEvent.java +++ b/shell/platform/android/test/io/flutter/util/FakeKeyEvent.java @@ -10,6 +10,9 @@ public FakeKeyEvent(int action, int keyCode) { } public final int getUnicodeChar() { + if (getKeyCode() == KeyEvent.KEYCODE_BACK) { + return 0; + } return 1; } }