G'day everyone, welcome back.
In this video, we'll look at some strange behavior you can get,
if you override a dialog's onDismiss function, but fail to call the super function.
This is going to rely on us putting the emulator into landscape mode,
so I'll start by checking the existing code,
to see what happens when we dismiss the dialog and change the orientation.
With the app running, I'll delete a task and get the dialogue to come up.
It doesn't matter how we dismiss it, so I'll just tap the screen away from the dialogue, to cancel it.
Okay.
I'll rotate the device into landscape.
That's fine.
It's behaving as we'd expect, so back into portrait.
Let's repeat the test, after commenting out the super.onDismiss line in the AppDialog class.
With the app running again, I'll again tap to delete a task,
and as expected, we get the dialogue.
Now, when you're running tests like this, to check the effect of a change,
it's important to perform exactly the same steps after the change, as you did before.
Otherwise, if you get different behavior,
you can't be sure that it's a result of the change, or the fact that you've done things differently.
So I'll dismiss the dialogue in the same way as I just did,
by tapping on the screen, away from the dialogue.
The dialogue's dismissed, as we expect.
I'll rotate the device into landscape again.
That's probably not what you expected. Well actually, maybe it is.
You may have guessed that I wouldn't be doing all this, if it was going to behave normally.
The question is, what state is everything in?
Is the DialogFragment still attached to the activity?
If so, tapping delete's going to delete the task.
If not, the dialogue will just disappear,
because we've used the safe call operator, when calling any callback functions.
So I've got a mini challenge for you.
You need to be able to see the logcat,
so I'll expand it a bit, and make sure at least the last 20 lines or so, are visible.
It will be easier to find what we want if I double-click AppDialog in one of the entries,
and then use ctrl F to highlight all the AppDialog entries.
The challenge is, what's going to happen when I tap the delete button.
Pause the video now, and use the logcat to work out what state everything's in,
and what the delete button will do.
Okay, here's the solution.
In the logcat, we can see onCancel being called, when I cancelled the dialogue.
The onDismiss is called straight after that.
Then there's a slight delay, as I talked a bit, and rotated the device into landscape,
and we can see onAttach being called.
So the DialogFragment has been attached to MainActivity again, after the rotation.
Now, if our AppDialog is attached to the activity, it'll be able to call the callback functions.
So, if you guessed that tapping the delete button would delete the task, congratulations.
I'll tap the button and see if we're right.
Oops, I just deleted all the records of three months work, and I haven't yet billed that client.
Fortunately, we followed Google's guidelines, and provided a good description of what would happen,
and we called the button delete.
It's unlikely the user would have tapped delete, in this case.
They're more likely to have used the cancel button.
If we'd made the message something inane like, "are you sure?"
and just provided YES and NO buttons,
they may have thought we were asking for confirmation of the device rotation,
or just been totally confused, and pressed the positive YES button.
Alright, if you do override the onDismiss function,
make sure you call the super.onDismiss function in your overridden function.
Otherwise, as I mentioned earlier, and we've just seen, strange things can happen.
Our onDismiss function is only being used for logging, so I'm going to delete it.
It's not a good idea to leave useless code like this, in a released version of an app,
and we've just had a good demonstration of why unnecessary code may contain bugs,
and it's better to remove it, rather than risk those bugs appearing in production.
See you in the next video.