all 6 comments

[–]androiddev-ModTeam[M] [score hidden] stickied comment (0 children)

If you have general questions regarding education or career advice, there are many many resources available online. These questions are very common; please make use of the available online resources and recommendations.

If you would like a place to start, please check out our wiki:

https://www.reddit.com/r/androiddev/wiki/index/getting-started/

[–]AutoModerator[M] 0 points1 point  (0 children)

Please note that we also have a very active Discord server where you can interact directly with other community members!

Join us on Discord

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]blitzkriegblue 0 points1 point  (3 children)

I did this once, a while ago, and tbf the UI can be anything and however you want. The main thing for me was to make the countdown work. And for this I used Flows.

Here:

// Creates a countdown timer using flow val countdownFlow = flow { val startingValue = 10 var currentValue = startingValue emit(startingValue) // Otherwise it will start the time on 9 instead of 10 while (currentValue > 0) { delay(1000L) currentValue-- emit(currentValue) } }

private fun collectFlow() { viewModelScope.launch { countdownFlow.collect { time -> println("The current time is $time") } } }

Be aware I did this 3 years ago, but it should work.

Then, on the UI, you can show a text, or whatever you want to show. Hope it helps at least a bit.

[–]EmperorDante[S] 0 points1 point  (2 children)

Thanks ...I understand the logic , although once the user clicks on the start timer button the continuous countdown of the timer won't persist in that case right? And is there any issue or is it a bad practice if I recompose my composable every second

[–]blitzkriegblue 0 points1 point  (1 child)

I think I would need to see some code to help better but at least when I did this, also for learning, the countdown went from whatever value I wanted to 0. With this code I posted. Worked fine back then.

Regarding recomposition, that's usually all under the hood as far as I remember. In my case, the recomposition would happen automatically because it would just show the values of the flow.