diff --git a/shell/platform/android/io/flutter/embedding/android/AndroidKeyProcessor.java b/shell/platform/android/io/flutter/embedding/android/AndroidKeyProcessor.java index 094b8ef1c53e8..14d7effe56c11 100644 --- a/shell/platform/android/io/flutter/embedding/android/AndroidKeyProcessor.java +++ b/shell/platform/android/io/flutter/embedding/android/AndroidKeyProcessor.java @@ -70,6 +70,15 @@ public AndroidKeyProcessor( this.keyEventChannel.setEventResponseHandler(eventResponder); } + /** + * Detaches the key processor from the Flutter engine. + * + *
The AndroidKeyProcessor instance should not be used after calling this. + */ + public void destroy() { + keyEventChannel.setEventResponseHandler(null); + } + /** * Called when a key up event is received by the {@link FlutterView}. * diff --git a/shell/platform/android/io/flutter/embedding/android/FlutterView.java b/shell/platform/android/io/flutter/embedding/android/FlutterView.java index 2f1942034a7e5..c5c92a05d9dc3 100644 --- a/shell/platform/android/io/flutter/embedding/android/FlutterView.java +++ b/shell/platform/android/io/flutter/embedding/android/FlutterView.java @@ -992,6 +992,8 @@ public void detachFromFlutterEngine() { textInputPlugin.getInputMethodManager().restartInput(this); textInputPlugin.destroy(); + androidKeyProcessor.destroy(); + if (mouseCursorPlugin != null) { mouseCursorPlugin.destroy(); } diff --git a/shell/platform/android/test/io/flutter/embedding/android/AndroidKeyProcessorTest.java b/shell/platform/android/test/io/flutter/embedding/android/AndroidKeyProcessorTest.java index 23369a2d3109b..8eddb009dc828 100644 --- a/shell/platform/android/test/io/flutter/embedding/android/AndroidKeyProcessorTest.java +++ b/shell/platform/android/test/io/flutter/embedding/android/AndroidKeyProcessorTest.java @@ -2,7 +2,9 @@ import static junit.framework.TestCase.assertEquals; import static org.mockito.Mockito.any; +import static org.mockito.Mockito.isNull; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.notNull; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -56,6 +58,22 @@ public void respondsTrueWhenHandlingNewEvents() { verify(fakeView, times(0)).dispatchKeyEvent(any(KeyEvent.class)); } + @Test + public void destroyTest() { + FlutterEngine flutterEngine = mockFlutterEngine(); + KeyEventChannel fakeKeyEventChannel = flutterEngine.getKeyEventChannel(); + View fakeView = mock(View.class); + + AndroidKeyProcessor processor = + new AndroidKeyProcessor(fakeView, fakeKeyEventChannel, mock(TextInputPlugin.class)); + + verify(fakeKeyEventChannel, times(1)) + .setEventResponseHandler(notNull(KeyEventChannel.EventResponseHandler.class)); + processor.destroy(); + verify(fakeKeyEventChannel, times(1)) + .setEventResponseHandler(isNull(KeyEventChannel.EventResponseHandler.class)); + } + public void synthesizesEventsWhenKeyDownNotHandled() { FlutterEngine flutterEngine = mockFlutterEngine(); KeyEventChannel fakeKeyEventChannel = flutterEngine.getKeyEventChannel();