Welcome back.
In the current iteration of our application, if we are inside of our exercise and we click on the back
button, then the exercise will close.
And this is maybe not something that the user wants, so this might be unintentional.
And in order to give him the option to say, Hey, this was unintentional.
What we can do is we can add a little custom dialogue that will then pop up, which allows the user
to say, Yes, I want to go back or no, I want to keep on doing the exercise.
So that's what we're going to implement now.
Let's get started.
Therefore, I have this little axiom L lay out, and you can, of course, create your own lay out for
this, but I'm going to go over it real quick.
So this is what I want to have.
I have this little text on top.
Then another text, which is a description, then I have a yes button at the no button.
So the no button should not send us back to the main activity.
And the yes button should send us back to the main activity, which means it should just finish the
current activity.
OK.
And that's what we want to achieve.
So now that you have this dialogue custom backed confirmation, some elford you could try to implement
this yourself.
We have used custom dialogues before, but of course I will go a little more into depth in this video,
so this could involve you having to research a little bit.
All right.
So as you see, this is what we want to achieve.
Let me go into the quote real quick.
You see, I'm just using a linear layout here with a little bit of padding, the gravity being centered,
items being vertically aligned inside of it.
We have a text view which says, Are you sure?
Then another text you with the description to stop your workout and so forth.
Then we have a linear layout for the two buttons that are next to each other, which is a horizontally
oriented layout.
And this is something you should try to avoid to have a linear layout inside of linear layouts when
it comes to having more complex applications.
That's why Android has introduced or Google has introduced constraint layouts.
But for such simple examples as we have here, this is still a fine approach.
Even though I would recommend that you try to basically build the same thing using a constraint layout
if you feel like it.
So now let's actually implement it, and where would be a good spot to implement it well inside of our
exercise activity?
OK, so here inside of the exercise, not the status adaptor, but the actual exercise activity, that's
where we want to implement this functionality.
So instead of using the UNbacked pressed method here, when we are in the toolbar exercise and we click
on the back button, what we want to do is we want to have our own method that will then.
This play saw custom dialogue for back button is hungering to call this method.
And now let's go ahead and create this method because I'm going to create a here which will make it
a little simpler.
And this will be a private, fun, custom dialogue for background or for button.
So first of all, we need to create a dialogue.
So custom dialogue is how I'm going to call it, and it will just be a dialogue with this as the context.
And so we need to import dialogue into our.
Project here.
You see that into our file to be more precise.
And then we can now set up this custom dialog.
OK, so first of all, I'm going to use data binding in order to prepare this dialogue.
So dialogue binding.
Finding, which will be dialogue, custom back conformation binding, so this is a tricky part that
we have not seen yet, and that is due to data binding.
If you want to inflate an XML file and you want to use its items such as the button, such as the well,
in our case, it's just going to be the two buttons.
But if you had other UI elements that you want to access as well.
So now we have the dialogue binding ready because the binding that we had so far, this binding object
that were created up here, activity exercise binding doesn't work because this XOM l file is not part
of the external file for our exercise activity, so it's a separate XML file.
So we need to create an own binding for it.
And you see, that's the name of the XML file, and that's also the name of the binding.
So here dialog custom confirmation is the name of the XML file, and we just have this binding keyword
and we inflate with the layout inflator.
Similarly to what we have done with the activity exercise binding.
OK, so now we just do it for the dialogue here and now we can use this dialogue binding in order to
access the items of it.
And first of all, we need to give our custom dialogue a content view to.
Make sure how it's going to look like.
So here we can use our dialogue binding route for that.
So we're just saying the custom dialogue should now look like our.
Exemplified five here.
OK, so we have used custom dialogues before, and if you used find you by ID and everything like that,
that's fine as well.
OK, so now we're setting the custom view content to you, and then we need to make sure that we have
a tencel on touch.
Outside to be false.
So we want to make sure that the user can not cancel this custom for back button dialogue, so let me
get back into the application and show you how this is going to look like.
So generally speaking, when I press the back button, you see, I cannot cancel this dialogue when
I click outside of it.
I only want to be able to cancel it when I click either no or yes.
OK.
So that's why we need this set cancel on touch outside.
And then we can go ahead and set the individual buttons, so dialogue binding, thought TV?
Yes.
So the text view, yes, or actually it's I think it's a button.
Check.
This year and the split, yeah, and we'll call the TVs, even though it should be called button, yes,
something like this button, yes.
And here also button no.
But the more descriptive because it's actually buttons, right?
So now we can access them.
So button, yes.
What should happen when we click on the button?
Yes, button so we can have an on click listener for that?
And then what should happen when we click on the button, though?
And we have an active listener for that, too.
OK.
So either way, no matter what these buttons will do, once we click on them, we need to display our
custom dialogue.
So let's show the custom dialog by using the show method.
So what should happen when we click on?
Yes.
Well, then we want to close the current activity.
So here as we are in the context of the dialogue, we need to make sure that we are closing this exercise
activity.
So exercise activity, those finish, can I just call the finish method and then we need to dismiss
our custom dialogue because otherwise the dialogue will still be active.
OK, dismiss it like so.
And if the user clicks on, no, we can just dismiss our custom dialogue.
So there we are.
So now we just need to make sure that we have it all handled.
Okay?
Custom dialogue for back button.
Now let's go ahead and test this, whether it's going to work for us or not.
So there we are.
Let's click on the back button.
You see the screen pops up.
We can click click.
No, nothing will happen.
We came to yes, and we will go back to our main screen.
So on back pressed, so you can override whatever should happen when is on back pressed button is pressed.
OK, because I tried it and I was confused that it didn't work to just click on the back button and
it wouldn't appear or it wouldn't show up my custom dialogue for back button at the beginning of the
video.
So now you know how you can get away around that.
So you override what should happen when you click on the back button.
And my case, I say, OK, I want the custom dialogue for back button function to be called, which
creates our custom dialogue.
And then I want to do whatever happens when you click on the press button.
So let's test this, and maybe I will have to get rid of the super.
OK, so let me click on back, OK, and let me comment this out, and let's run it again.
So we run it.
We click on the back button and you see, now we see the dialog and we can say yes.
Then we go back all we can say no and we don't go back.
So I had to let me actually go back because having all of these sounds in the background is annoying.
So here I have to actually get rid of the super unpack press because what that will do is it will close
the current or it will finish the current activity by default.
At least that's the default functionality, and you can override that, as you see by just writing your
own code into this on back press function.
So that's really the power of using override functions.
They just give us the capability to really modify whatever we want and even go deeper into how the default
functionality is and override it with our own stuff.
OK.
So that's it for this video and the next one, we're going to prepare to beam ice cream.
So see you there.