Blog Infos
Author
Published
Topics
,
Published
Posted By: Udit Verma
@Composable
fun TimerScreen() {
LaunchedEffect(key1 = Unit, block = {
try {
startTimer(5000L) { // start a timer for 5 secs
println("Timer ended")
}
} catch(ex: Exception) {
println("timer cancelled")
}
})
}
suspend fun startTimer(time: Long, onTimerEnd: () -> Unit) {
delay(timeMillis = time)
onTimerEnd()
}
The dictionary defines the term side-effect as an undesirable effect. While this applies to compose side-effects as well, sometimes they are required to mutate the state of the app. Side effects are undesirable because they can potentially change the state of the app outside the scope of the composable (global state).
@Composable
fun TimerScreen1() {
Column(
modifier = Modifier
.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally
) {
var timerDuration by remember {
mutableStateOf(1000L) // default value = 1 sec
}
Button({
timerDuration -= 1000
}) {
Text("-1 second")
}
Text(timerDuration.toString())
Button({
timerDuration += 1000
}) {
Text("+1 second")
}
Timer(timerDuration = timerDuration)
}
}
@Composable
fun Timer(timerDuration: Long) {
LaunchedEffect(key1 = timerDuration, block = {
try {
startTimer(timerDuration) {
println("Timer ended")
}
} catch (ex: Exception) {
println("timer cancelled")
}
})
}
The dictionary defines the term side-effect as an undesirable effect. While this applies to compose side-effects as well, sometimes they are required to mutate the state of the app. Side effects are undesirable because they can potentially change the state of the app outside the scope of the composable (global state).

Job Offers

Job Offers


    Android Test Automation Engineer

    Komoot
    Remote
    • Full Time
    apply now

OUR VIDEO RECOMMENDATION

, ,

From Scoped Storage to Photo Picker: Everything to know about Storage

Persistence is a core element of every mobile app. Android provides different APIs to access or expose files with different tradeoffs.
Watch Video

From Scoped Storage to Photo Picker: Everything to know about Storage

Yacine Rezgui
Android developer advocate
Google

From Scoped Storage to Photo Picker: Everything to know about Storage

Yacine Rezgui
Android developer ad ...
Google

From Scoped Storage to Photo Picker: Everything to know about Storage

Yacine Rezgui
Android developer advocat ...
Google

Jobs

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
In this part of our series on introducing Jetpack Compose into an existing project,…
READ MORE
blog

How to animate BottomSheet content using Jetpack Compose

Early this year I started a new pet project for listening to random radio…
READ MORE
blog
Yes! You heard it right. We’ll try to understand the complete OTP (one time…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu