https://kotlinlang.org logo
#compose
Title
# compose
a

Archie

07/22/2020, 11:50 AM
Hi, I was trying out compose and was wondering about its lifecycle and how to deal with it and its gotchas. The only place I found for documentation was this right here: https://foso.github.io/Jetpack-Compose-Playground/general/compose_lifecycle/ I was wondering if there was more. Or maybe its too early for this?
a

Adam Powell

07/22/2020, 1:42 PM
What would you like to know?
a

Archie

07/22/2020, 3:24 PM
I hope its fine to ask dumb questions:
onDispose { } will be called when the compose function isn’t part of the composition anymore.
When is a compose function no longer part of the composition? Is everytime that it content changes? Like in the example in the documentation I cited:
Copy code
@Composable
fun LifecycleDemo() {
        val count = state { 0 }

        Column {
            Button(onClick = {
                count.value++
            }) {
                Text("Click me")
            }

            if (count.value < 3) {
                onActive { Log.d("Compose", "onactive with value: " + count.value) }
                onDispose { Log.d("Compose", "onDispose because value=" + count.value) }
                Text("You have clicked the button: " + count.value.toString())
            }
        }
}
If I understood it correctly,
onDispose()
is attached to
Column()
and so when
count
gets to
3
it needs to "Recompose" (not really sure if thats the correct term) because
Text()
is no longer part of it and so
onDispose()
will be called? Did I understood it correctly? If that is the case (in a different and unrelated question) the
Column
before the
count == 3
and the
Column
after the
count == 3
are of different instance? Did I understood it correctly?
a

Adam Powell

07/22/2020, 4:27 PM
The way to think about it is that
onActive
and
onDispose
are themselves entities in the composition. If the surrounding condition is true, they enter the composition. If the surrounding condition becomes false, they leave the composition.
a

Archie

07/22/2020, 6:58 PM
Ahhh I see, they are more like seperate entities. I was thinking of them the wrong way. Thank you very much. ❤️