1 00:00:03,770 --> 00:00:10,130 G'day everyone, welcome back. Our app's now saving the timing data, and updating 2 00:00:10,130 --> 00:00:16,309 the display so that the user knows which task is being timed. It works well, as long as 3 00:00:16,309 --> 00:00:19,400 it keeps running. It won't work too well if the app is 4 00:00:19,400 --> 00:00:24,020 closed while a task is being timed. That's easy to fix, 5 00:00:24,020 --> 00:00:35,940 but first, let's see the problem. I'll run the app on my emulator again and start timing a task. 6 00:00:35,940 --> 00:00:39,640 That should have created a new row in the timings table. 7 00:00:39,640 --> 00:00:42,420 Let's have a look. 8 00:01:02,739 --> 00:01:09,010 We've got a new row in the table with a duration of zero. I won't stop the timing - we 9 00:01:09,010 --> 00:01:14,830 know that that works. Instead, I'll stop the app. That could happen if the users' 10 00:01:14,830 --> 00:01:20,619 battery dies, or if Android needs to stop it to reclaim resources. A user could be 11 00:01:20,620 --> 00:01:26,040 travelling to the next job, and using the phone as a sat-nav to get there, for example. 12 00:01:26,040 --> 00:01:30,900 It's quite possible that Android will close the TaskTimer app if the 13 00:01:30,900 --> 00:01:36,660 sat-nav program needs more memory. To simulate that, I can stop the TaskTimer 14 00:01:36,660 --> 00:01:43,920 from the multitasking button on the emulator. On all the devices, the tasks 15 00:01:43,920 --> 00:01:49,460 have an X in the top right corner. Tapping the X will close the app. 16 00:01:49,460 --> 00:01:54,369 On newer versions, you close the app by swiping it off the screen. I'll do that 17 00:01:54,369 --> 00:02:00,940 with TaskTimer. In the logcat, we can see the activity fragment and view model 18 00:02:00,940 --> 00:02:04,540 going through their life cycles as they get destroyed. 19 00:02:04,540 --> 00:02:10,180 The last thing that TaskTimerViewModel does, is run it's onCleared function. 20 00:02:10,180 --> 00:02:14,880 You might think that that's a good place to store the current timing, but it's not 21 00:02:14,880 --> 00:02:20,140 and we'll see why. In fact, you may not even get a logcat entry for the 22 00:02:20,140 --> 00:02:24,880 onCleared function, as happened here. Don't worry, that can happen, and we'll be 23 00:02:24,880 --> 00:02:30,940 talking more about that later. Alright, I'll start the app up again in the emulator. 24 00:02:30,940 --> 00:02:35,200 I'm simulating what will happen on a user's device here, so I won't run 25 00:02:35,200 --> 00:02:44,400 it from Android Studio. Instead I'll find it in the apps list and run it on the device. 26 00:02:44,400 --> 00:02:51,040 We can see the problem straight away. It reports No tasks being timed. 27 00:02:51,040 --> 00:02:54,760 That's not good, and you should be able to work out what will happen if 28 00:02:54,760 --> 00:02:58,690 I start timing another task. Have a think about what will happen in our 29 00:02:58,690 --> 00:03:03,430 database if I do that. Pause the video, and come back when you've worked out 30 00:03:03,430 --> 00:03:09,220 what will happen. Alright, let's see what happens. 31 00:03:09,220 --> 00:03:16,980 I'll long-tap tap another task to start timing it. It doesn't matter which task I choose - 32 00:03:16,980 --> 00:03:21,480 I could even start timing the same task again. The display updates. 33 00:03:21,490 --> 00:03:31,750 Let's check the database in the terminal panel. I've still got the command in the 34 00:03:31,750 --> 00:03:37,060 terminals buffer and the up arrow will repeat it. Well that's not good. There's, 35 00:03:37,060 --> 00:03:42,040 now, no way to update the duration for the previous task, and we've got a second 36 00:03:42,040 --> 00:03:47,110 row with a duration of zero. If a user charges for travelling time, they won't be 37 00:03:47,110 --> 00:03:51,940 able to charge for this journey. Losing a user's data is unforgivable, 38 00:03:51,940 --> 00:03:56,500 so we'd better fix it. We'll look at how we do that in the next video. 39 00:03:56,500 --> 00:03:59,180 See you there.