G'day everyone, welcome back. Our app's now saving the timing data, and updating
the display so that the user knows which task is being timed. It works well, as long as
it keeps running. It won't work too well if the app is
closed while a task is being timed. That's easy to fix,
but first, let's see the problem. I'll run the app on my emulator again and start timing a task.
That should have created a new row in the timings table.
Let's have a look.
We've got a new row in the table with a duration of zero. I won't stop the timing - we
know that that works. Instead, I'll stop the app. That could happen if the users'
battery dies, or if Android needs to stop it to reclaim resources. A user could be
travelling to the next job, and using the phone as a sat-nav to get there, for example.
It's quite possible that Android will close the TaskTimer app if the
sat-nav program needs more memory. To simulate that, I can stop the TaskTimer
from the multitasking button on the emulator. On all the devices, the tasks
have an X in the top right corner. Tapping the X will close the app.
On newer versions, you close the app by swiping it off the screen. I'll do that
with TaskTimer. In the logcat, we can see the activity fragment and view model
going through their life cycles as they get destroyed.
The last thing that TaskTimerViewModel does, is run it's onCleared function.
You might think that that's a good place to store the current timing, but it's not
and we'll see why. In fact, you may not even get a logcat entry for the
onCleared function, as happened here. Don't worry, that can happen, and we'll be
talking more about that later. Alright, I'll start the app up again in the emulator.
I'm simulating what will happen on a user's device here, so I won't run
it from Android Studio. Instead I'll find it in the apps list and run it on the device.
We can see the problem straight away. It reports No tasks being timed.
That's not good, and you should be able to work out what will happen if
I start timing another task. Have a think about what will happen in our
database if I do that. Pause the video, and come back when you've worked out
what will happen. Alright, let's see what happens.
I'll long-tap tap another task to start timing it. It doesn't matter which task I choose -
I could even start timing the same task again. The display updates.
Let's check the database in the terminal panel. I've still got the command in the
terminals buffer and the up arrow will repeat it. Well that's not good. There's,
now, no way to update the duration for the previous task, and we've got a second
row with a duration of zero. If a user charges for travelling time, they won't be
able to charge for this journey. Losing a user's data is unforgivable,
so we'd better fix it. We'll look at how we do that in the next video.
See you there.