It seems that a `ComponentActivity` makes a `Compo...
# compose
m
It seems that a
ComponentActivity
makes a
ComposeView: AbstractComposeView
its content and populates it with an
AndroidComposeView
. The
AndroidComposeView
child creates and owns a
Composition
and the materialised tree (
root: LayoutNode
) driven by the
Composition
via a
UiApplier
. The parent references the
Composition
of the child. I’m wondering why it’s set up this way. What’s the point of the parent view? The
Composition
and the materialised tree are both in the child. Why doesn’t Compose just make that the content of the activity?
s
You need a view to wrap Compose nodes, as Android does not know about Compose. The parent is there for a variety of somewhat questionable and historical reasons, mostly for lifecycle management. You can create a
ComposeView
without creating composition and you can create it in a state where it does not have all of the dependencies.
AndroidComposeView
is not created until the view is attached and the dependencies are provided from the hierarchy.
Dependencies such as
LifecycleOwner
etc
Overall, mostly tech debt and not a lot of good reasons to have it, we are slowly trying to move to a world where you can create composition outside of the view or detached from hierarchy, but it is not exactly easy.
m
@shikasd I see. Is this also a tech debt?
Copy code
// internal class CompositionImpl
private fun addPendingInvalidationsLocked(values: Set<Any>, forgetConditionalScopes: Boolean) {
    values.fastForEach { value ->
        if (value is RecomposeScopeImpl) {
            value.invalidateForResult(null)
        } else {
            addPendingInvalidationsLocked(value, forgetConditionalScopes)
            derivedStates.forEachScopeOf(value) {
                addPendingInvalidationsLocked(it, forgetConditionalScopes)
            }
        }
    }
    // ...
Or when is a
value
a
RecomposeScopeImpl
?
s
no, that's intentional, this is how
RecomposeScope.invalidate
works
m
@shikasd I see. I’ve followed the call chain but was unable to locate the code that adds an invalidated recompose scope to
snapshotInvalidations
. Could you please point me to it?
m
I see, thank you for the pointer!