Welcome back in this video, we're going to finalize the application and therefore we're going to use
the start activity for result functionality as well as we are going to basically send over the details
from our other activities to this result activity which needs to know, first of all, what's the name
of the user who won and then the score.
So we need to know how many questions were there in total, and we need to know how many questions were
answered correctly.
So these are the details we need to pass.
So let's go ahead and actually built this in our project.
Therefore, let's go ahead to our constants because our constants are going to contain constant variables.
And whenever you send data from one activity to another, it's good to store the name under which you
stored the details that you want to have in a constant.
So this will make a lot more sense once we get started with it.
So cons. I'm going to call this one user on the score name, and here it's common practice to use capitalized
letters and use on the scores when you have a name with multiple words.
OK, so here this will be of type string, and I'm going to say that the user name will be the underscore
user name.
So the string is how it will be stored internally, and that's the constant name that I'm using.
So I'm passing the user name and I'm also going to have to pass the total amount of questions.
So let's create the constant value total underscore questions, which will also be of type string,
and it will contain the value called total questions like so.
And then I need the correct answer.
So I will create another constable correct answers, which will be of type string also, and it will
have the string correct answers stored.
So these are just going to be the keys, so to speak, on the which we are storing.
So the location, so to speak, under which we are storing the details so that we can retrieve them
when we are moving data from one activity to another.
OK, so let's go over to our very first activity, our main activity, where we now need to send the
data over.
So once we click on the button and this intent starts, we are starting an activity.
So now what we're going to do is we're going to start this activity, but we're also going to pass additional
data so you can do that by using put extra on an intent.
So an intent can have extra information that you sent over from one activity to another, and you can
then retrieve the information on the other activity.
So here I'm going to use this extra method, and you can see there are many different overloads one
with a name, which is the key and then developing into by cha along and so forth, so you can send
over all kinds of data.
OK.
And here you can see what you can send over.
OK, so even see realizable and partial files, which is basically your way, how you can send entire
classes over, but we're not going to see that only later.
Now, it's not necessary.
So first of all, I will need to pass in a key here, which is the first entry in the put extra.
So I'm going to use this constant here because it's good practice.
Of course, I could have just written something like username here that would have been fine as well,
as long as I know on the other side that I also need to retrieve username.
But it's just good practice to use constants here because if I use hardcoded strings, I might run into
errors.
So here I will pass over the name.
And where do I get it from?
Well, I get it from the added text here, which is the added text where we can enter our name here.
So this is the added text.
So whatever I enter, here is what I want to put extra to the intent.
So each name dot text to string because I want to pass a string and another char sequence.
So now I'm sending over to data with this intent, and I'm sending it not to the results activity,
but to the one that I'm going to do with this intent, which is the questions activity.
So here in the questions activity, I can now retrieve this information so I can retrieve the name and
I'm going to create a new variable here private VAR, which will be my user name.
So any user name, which will be of type string multiple and I'm going to set that to null at first
and then inside of the on create method, I will use M username.
To set the value, and I get it from the intent, so intent that get string extra at the position of
constant thought username.
Okay, so this is how I can now use the intent that got me to this activity and to get its details at
the position of the user name.
And I'm using that string extra because it was a string that I sent over.
So here and the main activity, I used to put extra method with a string that I sent over.
So I need to retrieve it as a string by using the guitar string extra.
You could see that intent has a bunch of different gets, so get Boolean extra get byte extra.
Get into flow.
Double Extra.
So all of that exists, and this would be, of course, used depending on what kind of data you are
sending over.
OK, so now when would be a good point to send over the details?
So sending over how many questions I have answered correctly?
How many questions there are in total and the username?
Well, I'd say once we click on the button, so we have this on click method and we click on the button
submit and once we click on the button, currently we are setting the question here when we still have
questions left.
But if we don't have any questions left, that's where we are going over to the end result screen here.
We had this make text toast, which just said you made it to the end.
So instead of using this toast, I'm just going to now send the program over to this other activity
by creating a new intent, as always.
So here we need to use the intent class and we need to import it with using old enter.
We need to pass in the context which will be this, and we need to say to which activity we want to
go.
And I'm going to go to the result activity, which I can do like.
So.
OK, now this intent will need to get extra details.
So I'm going to put extra on.
On one hand, it's going to be the username that I'm sending over, which means I'm going to use this
name, username, global variable that I created.
I'm going to send over the correct answers, so constants thought correct answers.
So the amount of correct answers and that is stored in on em.
Correct answers.
I think we don't have that variable yet.
Let me check.
Do we have a variable?
No, we need to have a global variable, which stores how many answers we have correct.
So private VAR and correct answers, which will be of type event and we can set that to zero at the
beginning.
And now we need to increase or increment the amount of answers given that were correct.
And what would be a good spot to do that?
I'd say each time that we click on to.
An answer here when we get the correct answer.
Well, the thing is here we're checking if we got the incorrect answer.
So now we can have this else block where we have the correct answer, and here we will increment the
am correct answers by one.
So inside of my own, click once I click on the button.
Submit.
I'm and I'm in the House block, which means in the L's of I don't have the selected option position
being zero, so I'm not at the very first question, then I get the question that I need to display
and I'm checking if I chose the right answer, right?
And then depending on that, if I chose the wrong answer, which is to put this if statements checks,
I'm setting the color to read.
And if I'm selecting the right answer, which just as block, I say, OK, I have another correct answer.
And at the same time, I'm setting the correct answer being green after that.
OK.
So I don't have to put it in here because I want to set it green.
Anyways, if I selected it or if I didn't select, it's correct.
OK, so now let's get back to our submit button when we are at the end of the question.
So at this point, we don't have any questions left, right and we want to go to the final screen,
so we need to put extra and correct answers.
So here I am.
Correct answers.
And then finally, we need to put extra the total amount of questions.
So how many questions that we have?
And here I'm going to get that from my questions list, which is another bill.
So I need to add the question mark here that size.
So I'm just saying, OK, just say how ever many questions are there in total and sent that over?
So that's what that size gets.
And here we of course, need to start the activity with that intent that we had set up here.
And then finally, I need to finish this activity because I don't want the user to be able to jump back
to the questions screen.
He should only be able to when clicking the back button, be able to get back to the start screen.
All right.
So that is it from this site now and the results activity.
We need to retrieve all of this information and in order to use it, we need to first set up our user
interface views, which means I need to have a name which would be a text to you.
And by the way, I'm not creating them outside of this method is on create method because I don't have
any other methods.
Otherwise I would create global variables as we have done here.
You can see we created all of those text use globally outside of the create method.
But as I stated, we don't need that in our result activity because it's not going to have any separate
methods that will be required.
So we have this text you.
We find it by its ID. And here let me import text view and find it via our DOD ID Dot TV underscore
name.
OK, so it's this text view here username where in the results screen right now, right?
So this user name has to be.
Cotton, OK.
So now that we have that, I would like you to set up the rest of the.
Items yourself, so post a video now and set up the other taxpayers.
OK, I hope you post the video.
And now let's just put that up here.
Now the next challenge for you is.
Please use the intent, get strong extra, as we have done in the questions questioning you here.
Get that extra with the user name and assign it to the right text to you which one will be, it will
be this TV name, so please go ahead and try it.
OK, I hope you tried it, so I will just set up TV name the text to be the intent.
Dot get string.
Extra at the position of constant thought.
And this will be the user name.
And I'll also retrieve the other values, so the amount of total questions and correct answers, and
this will be a little tricky.
OK.
So you can try it, but it is a little different to what we've seen before.
OK, so let me just show you total question, and this will be a new variable.
So total questions will be from the intent as well.
And here we're not using the get string extra, but we're getting to get into extra.
And why is that?
Well, because if we look at it where we set it, it's down here.
We put extra details, but incorrect answer.
If you hover over it and you jump to it, actually, you see it's a type int.
And amount of total questions is also going to be an event.
So we need to get into extra this time at the position of Constance total question, we get this extra.
Now you see that our aid is not very happy and our studio wants to get some more details because get
into extra if you look at it needs a default value of type integer.
All right.
So let's give it that default value because we're going to say if we didn't get any value, we're going
to use the default value of zero.
So the total amount of questions would then be zero.
And then we need another one for the correct answers and that one will be gotten and its answer is plural.
That one will be gotten also via an entire extra.
So get into extra.
So with the constant thought, and we called it correct answers that we are and we need to pass and
a default value, I'm going to say the correct answers will be zero.
So you see, we're using this conference correct answers.
I could have also just used it like this, correct?
On the score answers that would have done the same thing.
But as I stated here, for example, I wrote correct answers.
So now my program would crash because it doesn't find this extra detail, or at least it would use the
default value instead, because we have started on the correct answers, which is why it's a really
good idea to use constants here.
Okay, which is way all to do with you.
Constants.
OK, so now we can use these details to display them in our text view score.
So here we can now say the text.
So now we just need to set up the score.
So this TV score text should be your score is and then it's going to be the correct answer as well as
well out of total answers or total questions that are set.
And then finally, we have this button.
You could see here.
Well, you can't see here.
But let me just get to the final screen.
This button here and what I want.
So I want this to sent me back to the main activity.
So to the screen, and I can I just say finish because this would close this activity entirely and would
jump over to the well, it was close the whole application, basically.
So we want to avoid that and we do that by.
Well, first of all, sitting in on click listener here beautifully and starting an activity where we
just go ahead and create the directly inside of this activity so you don't need to create content in
an extra line.
You can do it all in one line.
So here I am going over from this result activity to the main activity and here column call and class
the JAVI'S how you can do it.
So now let's actually see if any of that works.
And then we can add to John because that's not my name.
Let's select Argentina about select Australia.
So like Brazil instead of Brazil there, and I cannot go back.
Anyways, this is Belgium, and let's just click through to where the final spot you see now it says
finish and we are done.
So, hey, congratulations on your score is three out of nine.
And I wouldn't say that that's great, but at least that's what our application is going to show.
It's always going to be positive, right?
Which is great.
So now we can just click on finish and we can start from scratch.
All right.
So that's pretty much the application.
That's the quest.
Now it's your job to make changes to it.
First of all, to go to your constants and now create a bunch of other questions, use your own images,
create, I don't know, like 100 questions or so and then use some of them randomly and so forth.
OK, and so this will be a little more of a challenge.
See this as an actual challenge because doing this by yourself will actually force you to learn how
to do it.
I can just show you how it's generally done, and this is like learning a language where you step by
step.
Well, I'm just teaching you how to speak the words and how grammar works, but in the end, you have
to speak it yourself in order to really learn the language and to be able to speak to people you need
to speak.
That's how it is with coding.
In order to learn coding, you need to code and you need to try it by yourself and not just code along
like a code, some stuff for you.
And that would be too easy.
So good luck with that and feel free to share your results in your application with us once you have
finished it.