Is there hook/callback for “Compose screen/widget ...
# compose
k
Is there hook/callback for “Compose screen/widget has finished rendering”? In XML views, you could hook into GlobalLayoutListener. Searched slack and stackoverflow for “finish rendering Jetpack Compose” w/o luck. This is needed for the typical analytics info of “has the LazyList finished rendering” or “has the screen finished rendering” to determine when a user can interact with it.
a
+1 for that. We just do it on first composition in a disposable effect inside the content block of a loading content error composable. But it’s not ideal. There was an onshown modifier found on the web that may work but we didn’t end up using it
l
Maybe
Modifier.onGloballyPositioned
on the root component?
a
If you mean the callback after an element is drawn (which I believe
OnGlobalLayoutListener
doesn't provide), how about applying something like this to the composable?
Copy code
Modifier.drawWithContent {
    drawContent()
    // Drawn
}
k
sorry…OnGlobalLayoutListener is only for full screen drawn. .onGloballyPositioned sounds like a possibility, but I assume that’s after layout, but not necessarily drawn (e.g. image downloaded over network via Coil, etc.)
j
wouldn't just LaunchEffect do the job ?
r
There is really a need to do this in a way that becomes a standard. Typically in analytics, one sees: • time to screen first shown • time to interactive • time to fully rendered (for screens that perform subsequent actions which enrich the data displayed) Last year, AirBnB did a really nice set of articles on measuring screen performance. It included a LOT more metrics than just the above. A recipe for getting to a similar point with Compose would be nice for the community.
a
IIRC
OnGlobalLayoutListener
is also called after layout phase instead of draw phase so If you are ok with
OnGlobalLayoutListener
,
Modifier.onGloballyPositioned
should be the same.
z
There’s no callback for “on draw finished” in compose. However, any coroutines dispatched during the frame won’t resume until after the choreographer finishes performing traversals (ie layout and draw, including for compose) for that frame, so a
LaunchedEffect
could work as well.
880 Views