This is that dang Codelab again isn't it? I knew it looked familiar... I already answered a similar question here - but basically, when you run division
on the main thread (which you must be since you're messing with UI components), you're freezing the app because you're blocking the thread with Thread.sleep
The display can't actually update until your code has finished running, i.e. after you exit the division
function, because it's all running on the same thread, and the display update pass comes later. So this is what's actually happening:
- freeze the app for 1 second
- set the text as the result of
60 / 4
- it won't actually redraw until later, after your code has finished, so there's no visual change
- freeze the app for 1 second
- set the text as the result of
60 / 3
- again you won't see anything happen yet, but now it's going to show 60 / 3
instead of 60 / 4
, because you just updated the state of that TextView
- etc.
The last text you set is the result of 60 / 1
, and then your code finishes, so the system can finally get around to updating the display. So the first thing you see after the app stops freezing is 60
- it's not just the numerator, it's the last calculation from your loop.
If you want something to update while the app is running, there are lots of solutions like coroutines, CountdownTimer
s, post
ing runnables that execute at a specific time, etc. The answer I linked shows how to create a separate thread to run basically the same code on, so you can block it as much as you like without affecting the running of the app. The one thing you don't do is block the main thread like that Codelab example does. It's a bad Codelab