diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/TextInputChannel.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/TextInputChannel.java index 8432269407..6c77899c3a 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/TextInputChannel.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/TextInputChannel.java @@ -110,6 +110,17 @@ public class TextInputChannel { channel.setMethodCallHandler(parsingMethodHandler); } + /** + * Instructs Flutter to reattach the last active text input client, if any. + * + * This is necessary when the view heirarchy has been detached and reattached + * to a {@link FlutterEngine}, as the engine may have kept alive a text + * editing client on the Dart side. + */ + public void requestExistingInputState() { + channel.invokeMethod("TextInputClient.requestExistingInputState", null); + } + /** * Instructs Flutter to update its text input editing state to reflect the given configuration. */ diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/editing/TextInputPlugin.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/editing/TextInputPlugin.java index 68efb65142..476592669f 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/editing/TextInputPlugin.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/editing/TextInputPlugin.java @@ -90,6 +90,8 @@ public class TextInputPlugin { } }); + textInputChannel.requestExistingInputState(); + this.platformViewsController = platformViewsController; this.platformViewsController.attachTextInputPlugin(this); restartAlwaysRequired = isRestartAlwaysRequired(); diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/editing/TextInputPluginTest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/editing/TextInputPluginTest.java index 0342668e21..96bf9e39d2 100644 --- a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/editing/TextInputPluginTest.java +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/editing/TextInputPluginTest.java @@ -1,12 +1,15 @@ package io.flutter.plugin.editing; import android.content.Context; +import android.content.res.AssetManager; import android.provider.Settings; import android.util.SparseIntArray; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodSubtype; +import java.nio.ByteBuffer; + import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; @@ -18,16 +21,38 @@ import org.robolectric.shadow.api.Shadow; import org.robolectric.shadows.ShadowBuild; import org.robolectric.shadows.ShadowInputMethodManager; +import io.flutter.embedding.engine.FlutterJNI; import io.flutter.embedding.engine.dart.DartExecutor; import io.flutter.embedding.engine.systemchannels.TextInputChannel; +import io.flutter.plugin.common.JSONMethodCodec; +import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.platform.PlatformViewsController; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; @Config(manifest = Config.NONE, shadows = TextInputPluginTest.TestImm.class, sdk = 27) @RunWith(RobolectricTestRunner.class) public class TextInputPluginTest { + @Test + public void textInputPlugin_RequestsReattachOnCreation() { + // Initialize a general TextInputPlugin. + InputMethodSubtype inputMethodSubtype = mock(InputMethodSubtype.class); + TestImm testImm = Shadow.extract(RuntimeEnvironment.application.getSystemService(Context.INPUT_METHOD_SERVICE)); + testImm.setCurrentInputMethodSubtype(inputMethodSubtype); + View testView = new View(RuntimeEnvironment.application); + + FlutterJNI mockFlutterJni = mock(FlutterJNI.class); + DartExecutor dartExecutor = spy(new DartExecutor(mockFlutterJni, mock(AssetManager.class))); + TextInputPlugin textInputPlugin = new TextInputPlugin(testView, dartExecutor, mock(PlatformViewsController.class)); + + ByteBuffer message = JSONMethodCodec.INSTANCE.encodeMethodCall(new MethodCall("TextInputClient.requestExistingInputState", null)); + verify(dartExecutor, times(1)).send("flutter/textinput", message, null); + } + @Test public void setTextInputEditingState_doesNotRestartWhenTextIsIdentical() { // Initialize a general TextInputPlugin.