Modern Apps git

← Modern-Apps.git

library-ui: detach first-frame listener after onDraw returns

Commit
d79268caf89f166605cc981f7b569e00543d0b17
Author
Vayun Mathur <[email protected]>
When
2026-12-05 22:14 UTC (5 hours ago)
Parents
555ebda
FirstFrameGate.attach removed its OnDrawListener from inside
onDraw, which Android forbids (IllegalStateException: Cannot call
removeOnDrawListener inside of onDraw). Because MaActivity
installs this gate for every app, the dev builds crashed on
their first frame fleet-wide (contacts, calendar, ...).

Keep the one-shot semantics (the fired flag already guards
re-entry) but post the removal so it runs after dispatchOnDraw
returns.

Verified: :library:ui:lint :library:ui:lintVitalRelease
:library:ui:testDevUnitTest green.

Diff

library/ui/src/main/java/com/vayunmathur/library/ui/StartupTiming.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/StartupTiming.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/StartupTiming.ktindex 6dba44ddfd..f9396422d4 100644--- a/library/ui/src/main/java/com/vayunmathur/library/ui/StartupTiming.kt+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/StartupTiming.kt@@ -87,9 +87,10 @@ class FirstFrameGate {     fun attach(activity: Activity, view: View) {         view.viewTreeObserver.addOnDrawListener(object : ViewTreeObserver.OnDrawListener {             override fun onDraw() {-                view.viewTreeObserver.removeOnDrawListener(this)                 if (fired) return                 fired = true+                // removeOnDrawListener is illegal inside onDraw; detach next frame.+                view.post { view.viewTreeObserver.removeOnDrawListener(this) }                 StartupTiming.onFirstFrame(activity)                 runCatching { activity.reportFullyDrawn() }                 val blocks = pending.toList()