Welcome back in this video, we're going to set up the finish activity, which should come up once we're
done with the exercise, the you see here.
The exercises are going through really quickly and then we have this end screen where we say, congratulations,
you have done the workout and you're finished.
Workout would probably be a better sentence here.
And then we can press finish and we go back to the start screen.
So let's get started building that.
Therefore, I'm going to go over to my project, and the first thing that I want to do is I want to
make sure that here the rest time should have a rest timer duration variable, which will be multiplying
with 1000 in here.
So let's create this arrest timer duration variable here.
So private VAR timer duration, which will be a that I'm going to assign 10 to by default.
But in this case, I'm going to use one.
So the rest timer will just be one second, and I'm going to do the same thing for my exercise timer
duration.
So exercise timer duration, which will also be a long and I'm going to use it.
Let me see, I've created an error here.
I'm going to use it inside of this one here where I'm going to use to exercise time duration times 1000,
which will make it one second.
So basically I'm saying, OK, I want to make those one second long so that we can can get through the
different exercises and rest screens very quickly.
Otherwise, this will take forever for us to even get to this finish screen that we just saw.
So let's go ahead and create the new activity.
And of course, you can try to implement all of the code that you're going to require now by yourself,
because everything that we're going to do now is based on things that you have learned already.
You just have to think of the flow, like what is the point where you want to move to this other activity?
How do you want to make this activity look?
So design it index.html file and then also create content that will move you back to the start activity,
which means domain activity.
So that's pretty much it.
All right, so I wish you all the best if you want to try it yourself, and I would highly recommend
that you try it yourself because this stuff is something that we've done so many times at this point
that you should be able to do it yourself.
OK, so therefore, the first thing that I'm going to do is I'm going to copy in the activity finish
from my other project, and I'm going to quick to go over it because it really doesn't make sense for
me to explain it in detail because we have built many different layouts before.
And let me just put it in here.
You can see that I need to draw both here as well.
Let me add that this new trouble that will be the action done trouble.
So let me put that in here.
I see action done, which will be this check here.
OK, so we need to have this finish activity for that, let's create a finish activity activity.
And I'm going to create an empty activity, which I'm going to call finish activity.
And because I already created the layout file or copied it, I will not generate it.
OK, but you if you are building it yourself, you should generate the layout file.
OK, so now I have this finish activity and I will need to say that as the content view and all of the
good stuff.
But let's look at this file real quick.
So you see that we have this linear layout in which we then have a toolbar.
Then we have this text view, which I call TPY.
And then inside of it, we have another linear layout which will have an image view inside of it and
then underneath a bunch of text viewers and a button.
So this will just create this layout that we have here with the toolbar at the top.
A text view, then this image view with another image and inside of it, then the congratulations text
view another text view.
Here you have done the work out and for me, actually changed it to finish to work out.
So you have finished or completed, I think complete it would be the nicest keyword here.
So you have completed the work out and then you have this finish button, which has an I.D. of button
finish.
OK.
So everything else, you can really set it up however you want.
You could even also use a constraint layout here.
You don't have to use a linear layout with another linear layout, but you can.
Also, if you are too lazy to build your own layout, you can just download this file.
I believe you should, however, be able to build this yourself in your own way.
OK, so now inside of the finished activity, that's where the magic will happen that we need to set
up and we are going to require view binding.
OK, so private VAR binding, which will be the activity finish binding, so activity finish binding
Nullarbor object that I'm going to assign null to.
OK, and now we can set that as the content for you.
So first of all, we need to initialize it and this will be activity finish binding, inflate with the
layout inflator as we had seen before.
And then we can set the content view with binding that route.
So this is also something that we have done multiple times before, and this is generally how you set
up view binding with the content views.
And now it's going to use this a linear layout, meaning this activity X and L file.
Because this linear layout contains the entire XML structure for our Finnish activity, because it's
the root view or layout inside of this activity.
Finish binding.
OK, now let's set up the support action bar and we have set up an action bar before in our exercise
activity.
So let's look at how we did it here.
Let's actually just copy this part here and set it up as well.
So.
Copy it, go over to our finish activity and set it up here.
So now we just need to find the name of the toolbar that I'm using and the activity finish and I call
it toolbar finish activity.
So here binding me, see why that isn't Iraq right here?
Toolbar finish activity.
OK, so that's how the name was.
And now I'm checking if the action bar is not now and if it's not now, then we're going to set up the
display home up enabled.
And then we had this on click listener assigned to the toolbar.
Let me put it together, so it will be a little cleaner.
So now we can do the same thing in our finished activity.
So here this will, of course, be the toolbar finish activity.
Set navigation, I'll click listen there on back pressed.
So once we click the back button, it will call the on back pressed method.
Which is the same thing as if you were clicking on the back button and then we have the finish button
inside of the activity finish, which is this button right here?
Let me go over to the code or actually split and click on the finish button and it will jump to the
finish button here, as you see, actually.
So you double click on it on this finish button or once click on it, and it will give you the button
itself with its ID.
OK, so now let's go ahead and set up this button.
So binding that finish button with an all click listener so that we can actually close to current activity.
So if we do that, it will close the current finish activity.
But if we don't close the exercise activity, once we jump to the finish activity, then we have an
error or we will have a problem because we will jump back to the exercise activity, which by itself
doesn't have much to offer at this point.
So we need to go back to the start activity.
So you could, of course, have an intent here that sends you back to the main activity.
But then you are building a big stack of activities that are still active in that stack.
And by finishing the activity, you're closing the current screen.
And then in this case, what it would do, it would jump to the last existing and open activity, which
should be our main activity.
So let's go to our exercise activity and now find the right spot where we want to jump over to the finish
activity.
And I hope you figured it out.
It's basically going to be once we are done with the timer for our exercise.
So let's find the on finish method in here, and we have to we have one inside of the said progress
bar and then one inside of the exercise one.
So here, said Exercise Progress Bar.
There we have this unfinished method.
And we checked whether we are inside of the current position exercise thingy, right?
And then we also notified data changed.
So what we can do now is we can put all of this code and only do it once we still are as long as we
still have exercises.
And then.
In the alternative case where we currently say, toast, make text, congratulations, you have completed
the seven minute work out, let's get rid of that and actually finish our current activity, which will
close the exercise activity at this point.
And at the same time, I want to jump over to the exercise finish activity that would just set up.
So let's create an intent with this as the first parameter, which is the context and then the finish
activity, colon, colon, class java as the activity that we want to jump to.
And I'm getting an error with this intent here.
Let me see if it imported the wrong intent.
Class for me doesn't seem like it at its intent at all.
Or maybe it's underutilized.
So what we need is the content in tents.
Well, there it is, content in tent.
So let me see why that problem occurs.
So this OK.
Yeah.
So the problem here and this is something we have not seen yet, but this is something that is really
important as well, is we need to add more details to this to the this keyword because now this is not
going to be at this point, our class.
We think it is, it's not going to be the exercise activity.
But if you hover over it, you see it will be the countdown timer because this countdown timer is an
object itself.
So now when you're using that this keyword, you're always talking about the context of your current
class and the current class that we're creating.
Here is an object countdown timer.
So this entire code here, if you look at it and we put that into a separate line here like so this
entire code this year is an object that we're creating.
And if you're using that this keyword, then it will use the nearest class, so to speak, in which
it exists.
So if you have a nesting situation, then it will take the one that is in its current nesting state.
So if it did this and the countdown timer and let's say you had another class inside of work, create
another object inside of this countdown timer and then you use this, then it will be this internal
object.
And now, as we're using the countdown timer, it will be the countdown timer that is used by this.
So how do we get around this?
How can we use this with our exercise activity?
What we can do that by using this at exercise activity?
So you add this at plus the name of the activity class that you want to pass as the context.
So this will be the package context for our intent now.
And now we just need to make sure that we start this intent.
So start activity with this intent.
OK.
So now let's try it out.
So it will close the current exercise activity and will jump over to the exercise finish activity.
So let's jump into it and jumps through all of the exercises in as quick as possible fashion in our
case.
In one second per-, uh, exercise slash perm rest phase.
And then once it's fully done, it will.
Jump to the finish activity, hopefully we will see the.
And so Blank finished there we are, so that we have our end activity and click on the back button,
it will go back to the four mentioned or before existing activity that still is in the stack and in
our case, it's still going to be our main activity.
Now, if you for some reason would finish here as well and then you would finish the exercise activity
as well as the finish activity, then you would end up closing the entire application.
So don't do that because you want to make sure that the user stays inside of your app.
So you should always have one activity being active and running and not finished unless you want to
actually close the application when something happens.
OK, so that's pretty much it for this video, I hope you managed to build this by yourself or at least
some parts of it, even if you were struggling at some points and if you had to look things up and jump
back to all the lectures or even watched this video until the end.
I think it's really important that you try as many things as possible now because now you should be
really confident at least trying these standard things, such as using on clip listeners such as importing
or implementing view binding.
Using intense all of this stuff should be rather simple.
The only challenging part, I would say, would have been this this thing where you are moving to another
activity once the timer is done.
And this is something that is hopefully an issue where you got stuck because then you hopefully also
started searching for solutions and maybe found the solution yourself, because that's how you actually
learn programming, not just by watching me how I do this stuff, but by actually running into issues
and trying to fix them.
OK, so I hope you enjoyed this a little challenge.
See you in the next video.