Thread
#compose
    Archie

    Archie

    2 years ago
    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?
    Adam Powell

    Adam Powell

    2 years ago
    What would you like to know?
    Archie

    Archie

    2 years ago
    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:
    @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?
    Adam Powell

    Adam Powell

    2 years ago
    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.
    Archie

    Archie

    2 years ago
    Ahhh I see, they are more like seperate entities. I was thinking of them the wrong way. Thank you very much. ❤️