min
11/06/2025, 9:38 AMsetOnViewTreeOwnersAvailable method of the AndroidComposeView class does:
fun setOnViewTreeOwnersAvailable(callback: (ViewTreeOwners) -> Unit) {
val viewTreeOwners = viewTreeOwners
if (viewTreeOwners != null) {
callback(viewTreeOwners)
}
if (!isAttachedToWindow) {
onViewTreeOwnersAvailable = callback
}
}
It seems to me that
• if the mAttachInfo of an AndroidComposeView is not null, it isAttachedToWindow,
• mAttachInfo is assigned a nonnull value in the dispatchAttachedToWindow method,
• dispatchAttachedToWindow is called by the addTransientView and addViewInner methods,
• and addViewInner is called by the addView and addViewInLayout methods.
So something must call either so that this.isAttachedToWindow would be true, and then call this.onAttachedToWindow(), but I can’t figure out what coordinates these calls. Also, if these calls do happen in this order, this.onAttachedToWindow() would assign a nonnull value to this.viewTreeOwners. So wouldn’t the two fields be either true and !null, or false and null? But there are two separate if statements above, not one with an else clause. This suggests to me that
• this.viewTreeOwners can be null or not,
• and this.isAttachedToWindow can be true or false
independently, i.e. there are 4 possibilities. So if this.viewTreeOwners != null and this.isAttachedToWindow is false, this would call the callback with the current this.viewTreeOwners and then schedule another call, since the onAttachedToWindow method (which runs when the AndroidComposeView has been attached to a window) calls this.onViewTreeOwnersAvailable?.invoke(/* ... */).
Questions:
1. What does the method expect the callback: (ViewTreeOwners) -> Unit argument to do with the ViewTreeOwners anyway?
2. Why the !isAttachedToWindow check before saving the callback in this.onViewTreeOwnersAvailable?
3. What’s the relationship between this.viewTreeOwners and this.isAttachedToWindow? Are they actually fully independent?