1
00:00:05,480 --> 00:00:09,700
Okay, let's continue where we left off in the last video.

2
00:00:09,700 --> 00:00:12,920
Now that we've finished the code,
all the errors have gone,

3
00:00:13,160 --> 00:00:18,540
except for our OK and Cancel resource IDs
that we're using as the defaults.

4
00:00:18,540 --> 00:00:24,140
Rather than editing the res/value/strings.xml
file,

5
00:00:24,360 --> 00:00:26,140
click on ok,

6
00:00:28,760 --> 00:00:32,279
then drop down from the light bulb that appears.

7
00:00:32,279 --> 00:00:37,649
We want the first option, Create string value
resource ok.

8
00:00:37,649 --> 00:00:39,960
The resource name's filled in for us.

9
00:00:39,960 --> 00:00:44,780
All we need to do is to provide the resource
value, OK.

10
00:00:44,780 --> 00:00:48,470
We can do the same thing for the cancel.

11
00:00:48,470 --> 00:00:56,559
This time, the resource value is Cancel.

12
00:00:56,559 --> 00:01:01,989
Our code's looking good, and we've checked
to make sure that a bundle was passed in when

13
00:01:01,989 --> 00:01:04,519
our dialog fragment was created.

14
00:01:04,519 --> 00:01:09,080
We also check to see if the bundle contains
an ID and a message.

15
00:01:09,080 --> 00:01:15,260
The positive and negative button IDs are optional,
but the dialog ID and the message aren't.

16
00:01:15,260 --> 00:01:21,360
If either value is missing, getInt returns
0 and getString returns null.

17
00:01:21,360 --> 00:01:26,390
We check this and throw an exception, just as we did for the null bundle.

18
00:01:26,390 --> 00:01:32,950
I've made the exception messages a bit different,
so it's obvious where the exception was thrown.

19
00:01:32,950 --> 00:01:38,100
Remember that you won't always be running on a device that's connected to Android Studio.

20
00:01:38,100 --> 00:01:41,890
You may just have a log file to try to diagnose problems from.

21
00:01:41,890 --> 00:01:47,720
I haven't mentioned what happens in our onClick listeners for the two buttons.

22
00:01:47,720 --> 00:01:51,850
All they do is call the appropriate callback functions,

23
00:01:51,850 --> 00:01:55,380
using a safe call on dialogEvents.

24
00:01:55,380 --> 00:01:59,880
The dialog will automatically dismiss itself when one of the buttons is tapped,

25
00:01:59,880 --> 00:02:03,840
so we don't have to worry about calling its dismiss function.

26
00:02:03,850 --> 00:02:08,628
You will have to do that if you create a custom dialog,
and we'll look at that when we come

27
00:02:08,628 --> 00:02:10,810
to create a settings dialog.

28
00:02:10,810 --> 00:02:12,819
Okay, we're nearly done.

29
00:02:12,819 --> 00:02:17,860
There's just one more case we need to deal with,
and that's if the user dismisses the

30
00:02:17,860 --> 00:02:19,850
dialog using the back button.

31
00:02:19,850 --> 00:02:23,959
If that happens, neither of our callback functions will be called.

32
00:02:23,959 --> 00:02:29,709
What will happen, is that Android will call
the onCancel function in the dialog fragment

33
00:02:29,709 --> 00:02:30,709
class.

34
00:02:30,709 --> 00:02:33,670
It also calls the onDismiss function.

35
00:02:33,670 --> 00:02:38,680
We can override the onCancel and respond to it there.

36
00:02:38,680 --> 00:02:43,459
I'll get Android Studio to generate both functions
for us, and I'll put them right at the end

37
00:02:43,460 --> 00:02:48,180
of the class, after the onDetach function,
and add the usual logging.

38
00:03:17,180 --> 00:03:27,959
I'm also going to add a comment next to the
super call, in onDismiss.

39
00:03:27,959 --> 00:03:34,099
You could leave the call to super.onCancel
in the onCancel function, but it doesn't do

40
00:03:34,099 --> 00:03:40,549
anything, as you can see, by control clicking on it.

41
00:03:40,549 --> 00:03:47,139
The super.onDismiss call is needed, and if
you remove it, strange things happen; such

42
00:03:47,139 --> 00:03:51,529
as a dialog reappearing when the device is rotated.

43
00:03:51,529 --> 00:03:57,810
Control click the super.onDismiss
and you'll see that it does do something.

44
00:03:57,810 --> 00:04:02,180
The comment does mention that the user may
see the dialog again, when they come back

45
00:04:02,180 --> 00:04:03,299
to the activity.

46
00:04:03,299 --> 00:04:08,420
There's no mention of this in the Android
documentation that we just looked at.

47
00:04:08,420 --> 00:04:13,590
Let's go back and check that, because we need
to have a look at the last section anyway,

48
00:04:13,590 --> 00:04:19,769
to work out how we should code these two functions.

49
00:04:19,769 --> 00:04:32,100
So back in https://developer.android.com/guide/topics/ui/dialogs.html

50
00:04:32,100 --> 00:04:37,480
I'll scroll right down to the bottom of the document.

51
00:04:37,490 --> 00:04:40,300
It's discussing dismissing a dialog.

52
00:04:40,300 --> 00:04:46,220
When the user touches any of the action buttons
created with an AlertDialog.builder, the system

53
00:04:46,220 --> 00:04:48,380
dismisses the dialog for you.

54
00:04:48,380 --> 00:04:54,030
There are very rarely any wasted words in
technical documentation, so notice that it

55
00:04:54,030 --> 00:04:59,960
doesn't just say, when the user touches any
of the action buttons, the system dismisses

56
00:04:59,960 --> 00:05:01,820
the dialog for you.

57
00:05:01,820 --> 00:05:07,480
The fact that it specifically refers to the
buttons created with an AlertDialog.builder

58
00:05:07,480 --> 00:05:10,110
is important.

59
00:05:10,110 --> 00:05:15,730
And if you use your own buttons from a layout,
when using a custom dialog, the system won't

60
00:05:15,730 --> 00:05:18,080
dismiss the dialog for you.

61
00:05:18,080 --> 00:05:21,130
You'll have to call the dismiss function yourself.

62
00:05:21,130 --> 00:05:25,330
We'll see that when we create a settings dialog,
in a later video.

63
00:05:25,330 --> 00:05:31,240
Okay, if we need to perform any special actions
when the dialog goes away, we'll do that in

64
00:05:31,240 --> 00:05:33,410
the onDismiss function.

65
00:05:33,410 --> 00:05:38,870
There's often no need to override onDismiss,
and the only reason I've done it, is to log

66
00:05:38,870 --> 00:05:41,080
the fact that it's been called.

67
00:05:41,080 --> 00:05:44,260
So that we can check the sequence of things in the logcat.

68
00:05:44,260 --> 00:05:50,440
If a dialog's dismissed, then you'll either get one of the button events called, or onCancel.

69
00:05:50,440 --> 00:05:56,360
To put that another way, whenever a dialog
is dismissed, for whatever reason, the onDismiss

70
00:05:56,360 --> 00:05:57,910
function will be called.

71
00:05:57,910 --> 00:06:02,430
It won't give you any indication of why the
dialog was dismissed.

72
00:06:02,430 --> 00:06:05,560
You can just respond to the fact that it has
been.

73
00:06:05,560 --> 00:06:11,410
There probably aren't too many situations
where overriding onDismiss will be useful.

74
00:06:11,410 --> 00:06:17,640
onCancel, on the other hand, is the only way
to respond if the dialog's cancelled,

75
00:06:17,640 --> 00:06:23,900
either by the user pressing the back button,
or tapping the screen outside of the dialog.

76
00:06:23,910 --> 00:06:25,940
So this function is useful.

77
00:06:25,940 --> 00:06:31,820
It indicates that a user has removed your
dialog, without using either of the buttons;

78
00:06:31,820 --> 00:06:37,070
that they haven't made one of the choices
you presented them with, in other words.

79
00:06:37,070 --> 00:06:43,090
A dialog could also be cancelled by calling
the Dialog class's cancel function, and in

80
00:06:43,090 --> 00:06:46,650
that case, onCancel would also be called.

81
00:06:46,650 --> 00:06:51,840
That's possibly less useful, because if your
code's going to call the cancel function,

82
00:06:51,840 --> 00:06:55,690
then it already knows that the dialog's going
to be cancelled.

83
00:06:55,690 --> 00:06:59,400
It can be a way to put all the cancel code
in one place, though.

84
00:06:59,400 --> 00:07:03,060
That's what the note on the end of the doc
is saying.

85
00:07:03,060 --> 00:07:08,400
onDismiss is called, even if it was dismissed
as a result of being cancelled.

86
00:07:08,400 --> 00:07:14,270
onCancel is only called when the dialog's
cancelled by one of the three functions mentioned.

87
00:07:14,270 --> 00:07:20,090
If onCancel is called, onDismiss will also
be called, but the opposite isn't true.

88
00:07:20,090 --> 00:07:21,090
Alright.

89
00:07:21,090 --> 00:07:27,220
In our onCancel function we've overridden,
we can call back our listener, using its

90
00:07:27,220 --> 00:07:30,440
onDialogCancelled function.

91
00:07:40,020 --> 00:07:45,180
We don't bother passing the arguments bundle
back from this function, because it's unlikely

92
00:07:45,180 --> 00:07:46,660
to be useful.

93
00:07:46,660 --> 00:07:50,990
The bundle is still available using the getArguments
function.

94
00:07:50,990 --> 00:07:56,230
So if we change our mind while developing
the app, it won't be the end of the world.

95
00:07:56,230 --> 00:08:01,360
Notice that we don't bother checking if the
dialog ID is present in the bundle or even

96
00:08:01,360 --> 00:08:02,890
if there is a bundle.

97
00:08:02,890 --> 00:08:07,810
That checking was performed when our dialog
fragment was created, so there's no point

98
00:08:07,810 --> 00:08:09,180
checking it again.

99
00:08:09,180 --> 00:08:14,560
If the bundle didn't exist or didn't contain
an ID, then the program would have crashed

100
00:08:14,560 --> 00:08:16,650
before we could get here.

101
00:08:16,650 --> 00:08:21,720
Alright, that's how you respond to the dialog
being cancelled, if you wanted to.

102
00:08:21,720 --> 00:08:26,380
We're not doing that, and I've got an error
because our interface doesn't have an

103
00:08:26,380 --> 00:08:28,520
onDialogCancelled function.

104
00:08:28,520 --> 00:08:36,940
I'll comment that line out, because we're
not implementing the cancel callback.

105
00:08:36,940 --> 00:08:39,049
I'll stop the video here.

106
00:08:39,049 --> 00:08:44,820
In the next video, we'll use this AppDialog
class to allow the user to confirm the deletion

107
00:08:44,820 --> 00:08:46,570
of a task record.

108
00:08:46,570 --> 00:08:47,540
See you in the next one.

