Disconnect the view's AndroidKeyProcessor when detaching from the engine (#21307)

This commit is contained in:
Jason Simmons 2020-09-21 17:42:02 -07:00 committed by GitHub
parent 6a6d6d7593
commit 11befe419e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 0 deletions

View File

@ -70,6 +70,15 @@ public class AndroidKeyProcessor {
this.keyEventChannel.setEventResponseHandler(eventResponder);
}
/**
* Detaches the key processor from the Flutter engine.
*
* <p>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}.
*

View File

@ -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();
}

View File

@ -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();