diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index e5609d383c48..1b0614d6b966 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -6362,6 +6362,8 @@ public class com/facebook/react/views/textinput/ReactTextInputManager : com/face public fun getShadowNodeClass ()Ljava/lang/Class; public synthetic fun onAfterUpdateTransaction (Landroid/view/View;)V protected fun onAfterUpdateTransaction (Lcom/facebook/react/views/textinput/ReactEditText;)V + public synthetic fun onDropViewInstance (Landroid/view/View;)V + public fun onDropViewInstance (Lcom/facebook/react/views/textinput/ReactEditText;)V public synthetic fun receiveCommand (Landroid/view/View;ILcom/facebook/react/bridge/ReadableArray;)V public synthetic fun receiveCommand (Landroid/view/View;Ljava/lang/String;Lcom/facebook/react/bridge/ReadableArray;)V public fun receiveCommand (Lcom/facebook/react/views/textinput/ReactEditText;ILcom/facebook/react/bridge/ReadableArray;)V diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.kt index 99a753dbd362..1cc05cb47409 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.kt @@ -920,6 +920,11 @@ public open class ReactTextInputManager public constructor() : view.maybeUpdateTypeface() } + override fun onDropViewInstance(view: ReactEditText) { + TextLayoutManager.deleteCachedSpannableForTag(view.id) + super.onDropViewInstance(view) + } + override fun addEventEmitters(reactContext: ThemedReactContext, editText: ReactEditText) { editText.setEventDispatcher(getEventDispatcher(reactContext, editText)) editText.addTextChangedListener(ReactTextInputTextWatcher(reactContext, editText)) diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/textinput/ReactEditTextCachedSpannableTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/textinput/ReactEditTextCachedSpannableTest.kt new file mode 100644 index 000000000000..458eb693be70 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/textinput/ReactEditTextCachedSpannableTest.kt @@ -0,0 +1,65 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.react.views.textinput + +import android.text.Spannable +import android.util.DisplayMetrics +import androidx.core.content.res.ResourcesCompat.ID_NULL +import com.facebook.react.bridge.ReactTestHelper.createTestReactApplicationContext +import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests +import com.facebook.react.uimanager.DisplayMetricsHolder +import com.facebook.react.uimanager.StateWrapper +import com.facebook.react.uimanager.ThemedReactContext +import com.facebook.react.views.text.TextLayoutManager +import com.facebook.testutils.shadows.ShadowSoLoader +import org.assertj.core.api.Assertions.assertThat +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.kotlin.mock +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(shadows = [ShadowSoLoader::class]) +class ReactEditTextCachedSpannableTest { + + private lateinit var manager: ReactTextInputManager + private lateinit var view: ReactEditText + + @Before + fun setup() { + ReactNativeFeatureFlagsForTests.setUp() + val context = createTestReactApplicationContext(RuntimeEnvironment.getApplication()) + val themedContext = ThemedReactContext(context, context.baseContext, null, ID_NULL) + DisplayMetricsHolder.setScreenDisplayMetrics(DisplayMetrics()) + manager = ReactTextInputManager() + view = manager.createViewInstance(themedContext) + view.id = 42 + view.stateWrapper = mock() + } + + @Test + fun testDropViewInstanceRemovesCachedSpannable() { + view.text?.append("a") + assertThat(getSpannableCache()).containsKey(view.id) + + manager.onDropViewInstance(view) + + assertThat(getSpannableCache()).doesNotContainKey(view.id) + } + + @Suppress("UNCHECKED_CAST") + private fun getSpannableCache(): Map = + TextLayoutManager::class + .java + .getDeclaredField("tagToSpannableCache") + .apply { isAccessible = true } + .get(null) as Map +}