This commit is contained in:
coco
2026-07-15 16:57:36 +08:00
parent 723ce1af5c
commit 95aa018b8b
314 changed files with 8910 additions and 0 deletions
@@ -0,0 +1,53 @@
<!---
This is the API of version 1.2.0
-->
# 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.
```kotlin
@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:
* [Full Example Code]({{ site.samplefolder }}/general/LifecycleDemo.kt)