Skip to content
🤔 Documentation issue? Report or edit

Lifecycle

Compose has some “effects”-functions that can be used in Composables to track the lifecycle of a function.

  • LaunchedEffect {} will be called the first time a compose function is applied.

  • DisposableEffect { } Has a onDispose() which will be called when the compose function isn’t part of the composition anymore.

The example below has a Button that will count up everytime it gets clicked. When the count value gets 3, the Text() function will not be added anymore.

The first time the LifecycleDemo will be executed, the SideEffect in the if-clause will be executed. When the count value gets 3 +onDispose{} inside the if-clause will be called.

@Composable
fun LifecycleDemo() {
    val count = remember { mutableStateOf(0) }

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

        if (count.value < 3) {
            LaunchedEffect(Unit)
                Log.d("Compose", "onactive with value: " + count.value)
            }
            DisposableEffect(Unit) {
                onDispose {
                    Log.d("Compose", "onDispose because value=" + count.value)
                }
            }

            Text(text = "You have clicked the button: " + count.value.toString())
        }
    }
}

See also:


Last update: July 27, 2022