1
00:00:04,580 --> 00:00:06,700
G'day everyone, welcome back.

2
00:00:06,700 --> 00:00:10,500
In this video, we'll look at some strange behavior you can get,

3
00:00:10,500 --> 00:00:16,720
if you override a dialog's onDismiss function,
but fail to call the super function.

4
00:00:41,840 --> 00:00:46,300
This is going to rely on us putting
the emulator into landscape mode,

5
00:00:46,300 --> 00:00:49,300
so I'll start by checking the existing code,

6
00:00:49,300 --> 00:00:54,640
to see what happens when we dismiss the
dialog and change the orientation.

7
00:01:03,640 --> 00:01:09,820
With the app running, I'll delete a task
and get the dialogue to come up.

8
00:01:09,820 --> 00:01:16,320
It doesn't matter how we dismiss it, so I'll just
tap the screen away from the dialogue, to cancel it.

9
00:01:16,320 --> 00:01:17,360
Okay.

10
00:01:17,360 --> 00:01:22,500
I'll rotate the device into landscape.

11
00:01:22,500 --> 00:01:23,680
That's fine.

12
00:01:23,680 --> 00:01:29,100
It's behaving as we'd expect, so back into portrait.

13
00:01:29,100 --> 00:01:46,120
Let's repeat the test, after commenting out the
super.onDismiss line in the AppDialog class.

14
00:01:46,120 --> 00:01:51,340
With the app running again, I'll again tap to delete a task,

15
00:01:51,340 --> 00:01:54,300
and as expected, we get the dialogue.

16
00:01:54,300 --> 00:01:58,540
Now, when you're running tests like this,
to check the effect of a change,

17
00:01:58,540 --> 00:02:04,420
it's important to perform exactly the same
steps after the change, as you did before.

18
00:02:04,420 --> 00:02:06,940
Otherwise, if you get different behavior,

19
00:02:06,940 --> 00:02:12,040
you can't be sure that it's a result of the change,
or the fact that you've done things differently.

20
00:02:12,040 --> 00:02:15,780
So I'll dismiss the dialogue in the same way as I just did,

21
00:02:15,780 --> 00:02:19,580
by tapping on the screen, away from the dialogue.

22
00:02:19,580 --> 00:02:22,540
The dialogue's dismissed, as we expect.

23
00:02:22,540 --> 00:02:28,480
I'll rotate the device into landscape again.

24
00:02:28,480 --> 00:02:33,020
That's probably not what you expected.
Well actually, maybe it is.

25
00:02:33,020 --> 00:02:37,580
You may have guessed that I wouldn't be doing all this,
if it was going to behave normally.

26
00:02:37,580 --> 00:02:40,960
The question is, what state is everything in?

27
00:02:40,960 --> 00:02:44,560
Is the DialogFragment still attached to the activity?

28
00:02:44,560 --> 00:02:48,520
If so, tapping delete's going to delete the task.

29
00:02:48,520 --> 00:02:51,900
If not, the dialogue will just disappear,

30
00:02:51,900 --> 00:02:57,000
because we've used the safe call operator,
when calling any callback functions.

31
00:02:57,000 --> 00:02:59,240
So I've got a mini challenge for you.

32
00:02:59,240 --> 00:03:01,600
You need to be able to see the logcat,

33
00:03:01,600 --> 00:03:18,320
so I'll expand it a bit, and make sure at
least the last 20 lines or so, are visible.

34
00:03:18,320 --> 00:03:23,420
It will be easier to find what we want if I
double-click AppDialog in one of the entries,

35
00:03:23,420 --> 00:03:29,780
and then use ctrl F to highlight all the AppDialog entries.

36
00:03:29,780 --> 00:03:34,220
The challenge is, what's going to happen
when I tap the delete button.

37
00:03:34,220 --> 00:03:39,660
Pause the video now, and use the logcat
to work out what state everything's in,

38
00:03:39,660 --> 00:03:44,040
and what the delete button will do.

39
00:03:44,040 --> 00:03:45,980
Okay, here's the solution.

40
00:03:45,980 --> 00:03:51,900
In the logcat, we can see onCancel being called,
when I cancelled the dialogue.

41
00:03:51,900 --> 00:03:55,760
The onDismiss is called straight after that.

42
00:03:55,760 --> 00:04:00,980
Then there's a slight delay, as I talked a bit,
and rotated the device into landscape,

43
00:04:00,980 --> 00:04:04,200
and we can see onAttach being called.

44
00:04:04,200 --> 00:04:10,180
So the DialogFragment has been attached to MainActivity again, after the rotation.

45
00:04:10,180 --> 00:04:16,519
Now, if our AppDialog is attached to the activity,
it'll be able to call the callback functions.

46
00:04:16,519 --> 00:04:22,440
So, if you guessed that tapping the delete button
would delete the task, congratulations.

47
00:04:22,440 --> 00:04:28,940
I'll tap the button and see if we're right.

48
00:04:28,940 --> 00:04:35,540
Oops, I just deleted all the records of three months work,
and I haven't yet billed that client.

49
00:04:35,540 --> 00:04:41,120
Fortunately, we followed Google's guidelines,
and provided a good description of what would happen,

50
00:04:41,120 --> 00:04:43,920
and we called the button delete.

51
00:04:43,920 --> 00:04:47,580
It's unlikely the user would have tapped delete, in this case.

52
00:04:47,580 --> 00:04:50,800
They're more likely to have used the cancel button.

53
00:04:50,800 --> 00:04:54,880
If we'd made the message something inane like, "are you sure?"

54
00:04:54,880 --> 00:04:57,960
and just provided YES and NO buttons,

55
00:04:57,960 --> 00:05:02,480
they may have thought we were asking for
confirmation of the device rotation,

56
00:05:02,480 --> 00:05:06,620
or just been totally confused,
and pressed the positive YES button.

57
00:05:06,620 --> 00:05:11,060
Alright, if you do override the onDismiss function,

58
00:05:11,060 --> 00:05:17,280
make sure you call the super.onDismiss function
in your overridden function.

59
00:05:17,280 --> 00:05:22,540
Otherwise, as I mentioned earlier, and
we've just seen, strange things can happen.

60
00:05:22,540 --> 00:05:28,260
Our onDismiss function is only being used for logging,
so I'm going to delete it.

61
00:05:28,260 --> 00:05:33,480
It's not a good idea to leave useless code like this,
in a released version of an app,

62
00:05:33,480 --> 00:05:39,100
and we've just had a good demonstration of
why unnecessary code may contain bugs,

63
00:05:39,100 --> 00:05:50,000
and it's better to remove it, rather than
risk those bugs appearing in production.

64
00:05:50,000 --> 00:05:52,960
See you in the next video.

