diff --git a/shell/platform/android/io/flutter/embedding/android/AndroidKeyProcessor.java b/shell/platform/android/io/flutter/embedding/android/AndroidKeyProcessor.java index 094b8ef1c53..14d7effe56c 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 class 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 2f1942034a7..c5c92a05d9d 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 class FlutterView extends FrameLayout implements MouseCursorPlugin.MouseC 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 23369a2d310..8eddb009dc8 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 @@ package io.flutter.embedding.android; 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 class AndroidKeyProcessorTest { 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();