1 00:00:04,420 --> 00:00:06,640 G'day everyone, welcome back. 2 00:00:06,640 --> 00:00:10,660 Did you work out why the app crashes and manage to fix it? 3 00:00:10,660 --> 00:00:12,680 We've seen the error before. 4 00:00:12,680 --> 00:00:22,900 I'll run the app, and crash by editing a task, making a change and then using the Up button without saving. 5 00:00:39,900 --> 00:00:44,320 Because we included an informative message in the appDialog class, 6 00:00:44,320 --> 00:00:46,860 it's easy to see what the problem is. 7 00:00:46,860 --> 00:00:55,260 We have to implement the dialog's callback interface, 8 00:00:55,260 --> 00:01:02,820 and of course, we'll have to implement the function, which we'll do after the onBackPressed function. 9 00:01:20,120 --> 00:01:26,160 It's always worth checking the dialog ID, even if you've only got one dialog. 10 00:01:26,160 --> 00:01:30,440 Strange things could happen if someone adds another dialog later, 11 00:01:30,440 --> 00:01:33,840 and your callback function responds to any ID. 12 00:01:33,840 --> 00:01:36,920 Alright, that should stop the app from crashing, 13 00:01:36,920 --> 00:01:42,080 and we'll close the fragment if the user confirms that they want to abandon the edit. 14 00:01:42,080 --> 00:02:00,700 I'll run the app in portrait and make changes to one of the tasks. 15 00:02:00,700 --> 00:02:05,620 When I tap the Up button, we get the confirmation dialogue. 16 00:02:05,620 --> 00:02:11,660 CONTINUE EDITING just closes the dialogue, and the Edit fragment remains visible. 17 00:02:11,660 --> 00:02:15,760 I'll tap again. 18 00:02:15,760 --> 00:02:23,840 I'll tap again, and this time ABANDON CHANGES. 19 00:02:23,840 --> 00:02:35,740 That's good, now we'll test the Back button, 20 00:02:35,740 --> 00:02:37,840 and we get the same behavior. 21 00:02:37,840 --> 00:02:42,920 Tapping Back, after making a change, causes the dialogue to appear, 22 00:02:42,920 --> 00:02:47,980 and CONTINUE EDITING allows us to carry on making changes. 23 00:02:47,980 --> 00:02:54,600 Back again, and ABANDON CHANGES returns us to the main screen. 24 00:02:54,600 --> 00:02:58,020 The behavior's slightly different in landscape. 25 00:02:58,020 --> 00:03:04,020 Up works the same, but Back closes the app without requesting confirmation. 26 00:03:04,020 --> 00:03:06,880 You may not like that behavior of course. 27 00:03:06,880 --> 00:03:09,980 You're free to design your apps however you want. 28 00:03:09,980 --> 00:03:12,200 This is how we decided to do it, 29 00:03:12,200 --> 00:03:17,820 but if you want to change it, to get a confirmation if the user taps Back in landscape, 30 00:03:17,820 --> 00:03:23,220 then just remove the test for the mTwoPane, in onBackPressed. 31 00:03:23,220 --> 00:03:26,520 Okay, that's the confirmation dialogues. 32 00:03:26,520 --> 00:03:31,380 I wanted to show you how to use them from a fragment, and from an activity, 33 00:03:31,380 --> 00:03:35,340 which is why we're confirming leaving the addEditFragment. 34 00:03:35,340 --> 00:03:39,580 Google's UI guidelines suggest that that's not really a good idea. 35 00:03:39,580 --> 00:03:44,120 Google recommend that we should just save the data when the user taps the Up button, 36 00:03:44,120 --> 00:03:47,520 and cancel quietly if they use the Back button. 37 00:03:47,520 --> 00:03:51,080 Doing that would remove the need to have a Save button. 38 00:03:51,080 --> 00:03:58,560 We just call the saveTask function in AddEditFragment's onPause or onStop functions. 39 00:03:58,560 --> 00:04:03,840 I suggest you do that, because the app will work more like other Android apps, 40 00:04:03,840 --> 00:04:05,740 but don't do it yet. 41 00:04:05,740 --> 00:04:11,140 Wait until we finish the app, in case you introduce a bug when making the change. 42 00:04:11,140 --> 00:04:17,380 In the next video, we'll have a look at Kotlin extension functions and create a few more. 43 00:04:17,380 --> 00:04:20,279 See you in the next video.