Keep the Text accessibility delegate when the text has no links

## Summary:

On Android, every Text update goes through `updateExtraData` in the Text view manager (`PreparedLayoutTextViewManager`, and `ReactTextViewManager` for the older text view). There, the text's clickable spans are stored in a view tag and the accessibility delegate is reset, so that TalkBack can reach the links.

**Current behavior.** The comment says this is for a text view that "contains any clickable spans", but `PreparedLayoutTextViewManager` checks `is Spanned`, which is true for every Text, and `ReactTextViewManager` has no check at all. So every update of every Text creates a new `ReactTextViewAccessibilityDelegate`, even when TalkBack is off and the text has no links. Creating one is an `ExploreByTouchHelper`: it looks up the `AccessibilityManager`, makes the view focusable and then restores it, and allocates a `Handler` and a map. When the text has no links, nothing changes for the delegate, since it reads the links only when it's created.

**With this change.** Both managers call a new `ReactTextViewAccessibilityDelegate.updateAccessibilityLinks`. When the view already has a `ReactTextViewAccessibilityDelegate` and the text has no links, before or after the update, it keeps that delegate. In every other case (the first update, links added, changed or removed, or another delegate on the view) it resets the delegate as before. A plain Text still gets the delegate on its first update; `PreparedLayoutTextView` needs it to expose its text to TalkBack.

Setting a delegate through `ViewCompat` also makes a view whose `importantForAccessibility` is `auto` important. The kept path does the same, so a Text set back to `auto` (by the prop, or by view recycling) still ends up important, as before. The one difference: the accessibility actions a delegate caches from the `accessibilityActions` prop now stay between text updates, as they already do for every other view, instead of being dropped with the old delegate.

## Changelog:

[ANDROID] [CHANGED] - Text updates keep the accessibility delegate when the text has no links

## Test Plan:

- The pinned ktfmt (`--dry-run --set-exit-if-changed`) passes on all changed files.
- Added `ReactTextViewAccessibilityDelegateTest` (4 Robolectric tests: the first update installs the text delegate; updates without links keep the same delegate; adding and removing links resets it; a view set back to `auto` is important after an update). With the old code only "updates without links keep the delegate" fails. `PreparedLayoutTextViewAccessibilityTextTest` and `ReactTextViewPropertyTest` still pass.
- **Device:** Samsung Galaxy A16 (SM-A165F, Android 16), RNTester built with Gradle in Release (`assembleRelease`, arm64-v8a) and compiled with `cmd package compile -m speed`. The scenarios are a local Playground screen, not part of this PR: 200 numbers that change every 100 ms, and 60 lines of mixed-script text (emoji, CJK, Arabic, Hebrew, Polish) that change every 100 ms.
- **A/B setup:** a single APK with a local switch (an `adb shell setprop`, not part of this PR) that runs either the old reset on every update or this change. Launches alternate A B B A A B B A, 4 per arm and scenario; 15 s Perfetto traces with the app's trace sections. The changes are medians.

| | before | this PR | change |
|---|--:|--:|--:|
| `updateExtraData` per call, numbers | 36.6–39.1 µs | 28.4–30.0 µs | −21% |
| Mount per commit, numbers | 12.65–13.20 ms | 10.74–11.35 ms | −13% |
| Main thread CPU in 15 s, numbers | 3,614–3,654 ms | 3,484–3,625 ms | −3% |
| `updateExtraData` per call, mixed scripts | 48.3–55.4 µs | 38.5–41.1 µs | −28% |
| Mount per commit, mixed scripts | 5.49–6.06 ms | 4.79–5.07 ms | −18% |
| Main thread CPU in 15 s, mixed scripts | 2,858–2,950 ms | 2,804–2,849 ms | −4% |

These times include the cost of the trace sections themselves, about half of `updateExtraData`'s samples in a sampled profile, so without tracing the relative gain is larger.

- **Accessibility:** `uiautomator dump` of `TextExample`, `AccessibilityExample` and `AccessibilityAndroidExample`, 4 scroll positions each, with the old and the new code: the same 166 nodes, with the same text, content description and focusable, clickable and important-for-accessibility flags (positions differ only by the scroll). I couldn't run TalkBack on the device I used.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Differential Revision: D130917594

Pull Request resolved: https://github.com/react/react-native/pull/59521
This commit is contained in:
Gregory Moskaliuk authored and GitHub committed 2026-11-13 14:57:14 +00:00
1 parent 9b0200e703
commit 502f112fa3
4 files changed
+120 -32

No files matched your search

@@ -12,7 +12,6 @@ package com.facebook.react.views.text
import android.text.Spannable
import android.text.Spanned
import android.view.View
import com.facebook.react.R
import com.facebook.react.bridge.Dynamic
import com.facebook.react.internal.SystraceSection
import com.facebook.react.module.annotations.ReactModule
@@ -33,7 +32,6 @@ import com.facebook.react.uimanager.style.BorderRadiusProp
import com.facebook.react.uimanager.style.BorderStyle
import com.facebook.react.uimanager.style.LogicalEdge
import com.facebook.react.uimanager.style.Overflow
import com.facebook.react.views.text.ReactTextViewAccessibilityDelegate.AccessibilityLinks
import com.facebook.react.views.view.ImportantForInteractionHelper
import java.util.HashMap
@@ -77,20 +75,9 @@ internal class PreparedLayoutTextViewManager :
val preparedLayout = extraData as PreparedLayout
view.preparedLayout = preparedLayout
// If this text view contains any clickable spans, set a view tag and reset the accessibility
// delegate so that these can be picked up by the accessibility system.
if (preparedLayout.layout.text is Spanned) {
val spannedText = preparedLayout.layout.text as Spanned
val accessibilityLinks = AccessibilityLinks(spannedText)
view.setTag(
R.id.accessibility_links,
if (accessibilityLinks.size() > 0) accessibilityLinks else null,
)
ReactTextViewAccessibilityDelegate.resetDelegate(
view,
view.isFocusable,
view.importantForAccessibility,
)
val text = preparedLayout.layout.text
if (text is Spanned) {
ReactTextViewAccessibilityDelegate.updateAccessibilityLinks(view, text)
}
}
}
@@ -59,7 +59,11 @@ internal class ReactTextViewAccessibilityDelegate(
}
}
fun resetDelegate(view: View, originalFocus: Boolean, originalImportantForAccessibility: Int) {
private fun resetDelegate(
view: View,
originalFocus: Boolean,
originalImportantForAccessibility: Int,
) {
ViewCompat.setAccessibilityDelegate(
view,
ReactTextViewAccessibilityDelegate(
@@ -69,6 +73,33 @@ internal class ReactTextViewAccessibilityDelegate(
),
)
}
/**
* Stores the links among the clickable spans of [text] in a view tag and resets the
* accessibility delegate so that these can be picked up by the accessibility system. A view
* that already has this delegate keeps it when the text has no links, before or after the
* update, as the delegate only reads the links when it's created.
*/
fun updateAccessibilityLinks(view: View, text: Spanned) {
val accessibilityLinks = AccessibilityLinks(text)
val hadLinks = view.getTag(R.id.accessibility_links) != null
view.setTag(
R.id.accessibility_links,
if (accessibilityLinks.size() > 0) accessibilityLinks else null,
)
if (
accessibilityLinks.size() == 0 &&
!hadLinks &&
ViewCompat.getAccessibilityDelegate(view) is ReactTextViewAccessibilityDelegate
) {
// Like setting the delegate through ViewCompat, which makes an "auto" view important.
if (view.importantForAccessibility == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
view.importantForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_YES
}
return
}
resetDelegate(view, view.isFocusable, view.importantForAccessibility)
}
}
override fun onVirtualViewKeyboardFocusChanged(virtualViewId: Int, hasFocus: Boolean) {
@@ -18,7 +18,6 @@ import android.text.TextUtils
import android.text.util.Linkify
import android.view.Gravity
import com.facebook.common.logging.FLog
import com.facebook.react.R
import com.facebook.react.bridge.Dynamic
import com.facebook.react.common.ReactConstants
import com.facebook.react.common.annotations.UnstableReactNativeAPI
@@ -99,20 +98,7 @@ public constructor(
val update = extraData as ReactTextUpdate
val spanned: Spanned = update.text
view.setText(update)
// If this text view contains any clickable spans, set a view tag and reset the accessibility
// delegate so that these can be picked up by the accessibility system.
val accessibilityLinks: ReactTextViewAccessibilityDelegate.AccessibilityLinks =
ReactTextViewAccessibilityDelegate.AccessibilityLinks(spanned)
view.setTag(
R.id.accessibility_links,
if (accessibilityLinks.size() > 0) accessibilityLinks else null,
)
ReactTextViewAccessibilityDelegate.resetDelegate(
view,
view.isFocusable,
view.importantForAccessibility,
)
ReactTextViewAccessibilityDelegate.updateAccessibilityLinks(view, spanned)
}
}
@@ -0,0 +1,84 @@
/*
* 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.text
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ClickableSpan
import android.view.View
import androidx.core.view.AccessibilityDelegateCompat
import androidx.core.view.ViewCompat
import com.facebook.react.R
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
@RunWith(RobolectricTestRunner::class)
class ReactTextViewAccessibilityDelegateTest {
private val view = PreparedLayoutTextView(RuntimeEnvironment.getApplication())
@Test
fun `the first update installs the text delegate`() {
ReactTextViewAccessibilityDelegate.updateAccessibilityLinks(view, SpannableString("Hello"))
assertThat(delegate()).isInstanceOf(ReactTextViewAccessibilityDelegate::class.java)
assertThat(view.getTag(R.id.accessibility_links)).isNull()
}
@Test
fun `updates without links keep the delegate`() {
ReactTextViewAccessibilityDelegate.updateAccessibilityLinks(view, SpannableString("Hello"))
val first = delegate()
ReactTextViewAccessibilityDelegate.updateAccessibilityLinks(view, SpannableString("World"))
assertThat(delegate()).isSameAs(first)
}
@Test
fun `adding and removing links resets the delegate`() {
ReactTextViewAccessibilityDelegate.updateAccessibilityLinks(view, SpannableString("Hello"))
val plain = delegate()
ReactTextViewAccessibilityDelegate.updateAccessibilityLinks(view, textWithLink())
val withLink = delegate()
assertThat(withLink).isNotSameAs(plain)
assertThat(view.getTag(R.id.accessibility_links)).isNotNull()
ReactTextViewAccessibilityDelegate.updateAccessibilityLinks(view, SpannableString("Hello"))
assertThat(delegate()).isNotSameAs(withLink)
assertThat(view.getTag(R.id.accessibility_links)).isNull()
}
@Test
fun `a view set back to auto is important for accessibility after an update`() {
ReactTextViewAccessibilityDelegate.updateAccessibilityLinks(view, SpannableString("Hello"))
view.importantForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO
ReactTextViewAccessibilityDelegate.updateAccessibilityLinks(view, SpannableString("World"))
assertThat(view.importantForAccessibility).isEqualTo(View.IMPORTANT_FOR_ACCESSIBILITY_YES)
}
private fun delegate(): AccessibilityDelegateCompat? = ViewCompat.getAccessibilityDelegate(view)
private fun textWithLink(): Spanned =
SpannableString("Read the docs here").apply {
setSpan(
object : ClickableSpan() {
override fun onClick(widget: View) = Unit
},
9,
13,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE,
)
}
}