G'day everyone, welcome back.
Did you work out why the app crashes and manage to fix it?
We've seen the error before.
I'll run the app, and crash by editing a task, making a change and then using the Up button without saving.
Because we included an informative message in the appDialog class,
it's easy to see what the problem is.
We have to implement the dialog's callback interface,
and of course, we'll have to implement the function, which we'll do after the onBackPressed function.
It's always worth checking the dialog ID, even if you've only got one dialog.
Strange things could happen if someone adds another dialog later,
and your callback function responds to any ID.
Alright, that should stop the app from crashing,
and we'll close the fragment if the user confirms that they want to abandon the edit.
I'll run the app in portrait and make changes to one of the tasks.
When I tap the Up button, we get the confirmation dialogue.
CONTINUE EDITING just closes the dialogue, and the Edit fragment remains visible.
I'll tap again.
I'll tap again, and this time ABANDON CHANGES.
That's good, now we'll test the Back button,
and we get the same behavior.
Tapping Back, after making a change, causes the dialogue to appear,
and CONTINUE EDITING allows us to carry on making changes.
Back again, and ABANDON CHANGES returns us to the main screen.
The behavior's slightly different in landscape.
Up works the same, but Back closes the app without requesting confirmation.
You may not like that behavior of course.
You're free to design your apps however you want.
This is how we decided to do it,
but if you want to change it, to get a confirmation if the user taps Back in landscape,
then just remove the test for the mTwoPane, in onBackPressed.
Okay, that's the confirmation dialogues.
I wanted to show you how to use them from a fragment, and from an activity,
which is why we're confirming leaving the addEditFragment.
Google's UI guidelines suggest that that's not really a good idea.
Google recommend that we should just save the data when the user taps the Up button,
and cancel quietly if they use the Back button.
Doing that would remove the need to have a Save button.
We just call the saveTask function in AddEditFragment's onPause or onStop functions.
I suggest you do that, because the app will work more like other Android apps,
but don't do it yet.
Wait until we finish the app, in case you introduce a bug when making the change.
In the next video, we'll have a look at Kotlin extension functions and create a few more.
See you in the next video.