[SOLVED] How to change the Default value of a variable after some time?

Issue

This Content is from Stack Overflow. Question asked by reza zeraati

Is it possible in KOTLIN or by remember in Jetpack Compose to change the value of a variable after some seconds?

For example, I have a variable var currentResult1 = remember { mutableStateOf(true) }.
How can I say that after my activity opens, this currentResult1.value change to false after 1 second?



Solution

Yes, you can change value of any MutableState value using LaunchedEffect or coroutineScope builder functions.

LaunchedEffect(Unit) {
    delay(1000)
    currentResult1.value = false
}

You can check out this answer how to use it for a timer that changes current value every second.


This Question was asked in StackOverflow by Reza Zeraati and Answered by Thracian It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?