Announcements silently dropped when View is detached or invisible: android.view.View.announceForAccessibility
0 reputation · 13 Nov 2024, 11:21 UTC
0 reputation · 13 Nov 2024, 11:21 UTC
I need to reliably announce transient state changes to screen-reader users from a view that may be detached from the window or temporarily invisible. The documented failure condition is that android.view.View.announceForAccessibility sends a TYPE_ANNOUNCEMENT event, but if the view is not attached to a window or not visible, the event is never dispatched and the user hears nothing.
Additional uncertainty comes from the lack of a completion callback or delivery confirmation, leaving callers unable to know whether an announcement was spoken, throttled, or discarded. Speech interruption behavior is also screen-reader dependent, and event throttling, window state checks, and TalkBack handling vary across Android releases. Because these factors are version-sensitive, a single-device test is insufficient.
What is the supported pattern for guaranteeing dispatch when the target view may be detached or hidden? How should apps account for interruption and coalescing without delivery confirmation? What cross-version verification approach reliably confirms that announcements reach the user?
View.announceForAccessibility silently drops announcements when the view is detached from the window or not visible. The framework only routes TYPE_ANNOUNCEMENT events from views that are attached and visible, so a transient view that disappears before the event is dispatched will produce no speech.
When the target view may be detached or hidden, use one of these approaches:
TYPE_ANNOUNCEMENT. Obtain an AccessibilityEvent with TYPE_ANNOUNCEMENT, add the text, and send it through the AccessibilityManager service so the event does not depend on the target view's window attachment. Because the public surface for direct event injection has shifted across releases, confirm the exact method available in your compileSdk before relying on it.TextView in your activity root) with android:accessibilityLiveRegion="polite" and update its text when state changes. The framework announces the change automatically, and the view never detaches.There is no delivery callback. Screen readers process announcements asynchronously and may interrupt or coalesce rapid messages. Treat announcements as fire-and-forget:
AccessibilityManager.interrupt() only for critical, time-sensitive messages, because it stops whatever the user is currently hearing.Because TalkBack behavior and OEM accessibility suites vary, verify on multiple API levels and devices:
GONE or detached.adb shell dumpsys accessibility.One diagnostic detail would change the recommendation: is the announcement triggered from an Activity/Fragment context where a persistent root view exists, or from a background service where a live region is not available?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.