Post

A response to the section on "How Can I Determine the Size and Position of Descendants of Peers?" Using onGloballyPositioned (or onSizeChanged, or similar modifiers) like this is a quick way to get yourself into trouble with layouts that take multiple frames to resolve that can flash or look janky.

Mark Murphy@commonsware.com

I just sent out jetc.dev Newsletter Issue #273! Android Makers! @eevis.codes on accessibility focus *and* keyboard focus! FABs and FAMs! PWAs! jetc.dev/issues/273 #JetpackCompose #AndroidDev

When you have composition being driven by information that you can only get in a later phase, like measurement, layout or drawing, the first composition can't be correct! This means that the first frame might be blank, or components might jump around as you get more info.

Nesting this approach makes the situation even worse - each time you put a calculation like this inside another one, it adds an additional frame required to resolve the whole layout.

0 reposts0 likes

This can also wreck havoc on animations - maybe they can cover up some of the jankiness, but animations need to have a consistent view of the layout as it animates at each point in time. Shared element transitions don't really work if the shared element disappears completely for a frame.

Unfortunately there's not really a one-size-fits-all solution for every case, but don't be afraid of a custom layout - if you're already doing some math to change the layout of something based on something else, you're about 80% of the way there to writing a custom layout.

I'm curious how prevalent the onGloballyPositioned pattern is and why - does it feel like the best way to accomplish more custom layout behavior? Is it the easiest thing to get something working close enough? Are there missing tools that would make it less likely you'd use onGloballyPositioned?