A naive question about Kotlin Android

Just started learning Kotlin a moment ago, and I really need some help!

In the main(first) window, I want it to show a number, and this number should increase one per second automatically, and when I jump to another window, this number should still keep increasing. I have no idea how to implement this and stuck this question about two days, though just wanted to practice textview at first

thanks anyway

1 Like

This probably won’t make sense until you research the subject more, but I’d suggest creating a coroutine in a ViewModel that updates a StateFlow every second.

viewModelScope.launch {
    var i = 0
    while (isActive) {
        stateFlow.value = i
        i++
        delay(1000)
    }
}
1 Like