From 2377864d5655b09b57fd9064d5438dddc2d303eb Mon Sep 17 00:00:00 2001 From: Qichen Zhu <57348009+QichenZhu@users.noreply.github.com> Date: Thu, 10 Oct 2024 22:00:35 +1300 Subject: [PATCH] Set TextInput selection correctly when attached to window in Android --- .../com/facebook/react/views/textinput/ReactEditText.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java index ebd62e6dfa005a..87da1f00fc2d17 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java @@ -1053,12 +1053,18 @@ public void onStartTemporaryDetach() { public void onAttachedToWindow() { super.onAttachedToWindow(); + int selectionStart = getSelectionStart(); + int selectionEnd = getSelectionEnd(); + // Used to ensure that text is selectable inside of removeClippedSubviews // See https://github.com/facebook/react-native/issues/6805 for original // fix that was ported to here. super.setTextIsSelectable(true); + // Restore the selection since `setTextIsSelectable` changed it. + setSelection(selectionStart, selectionEnd); + if (mContainsImages) { Spanned text = getText(); TextInlineImageSpan[] spans = text.getSpans(0, text.length(), TextInlineImageSpan.class);