Welcome back and this video, I would like to introduce activity for result, because we are going to
require that for quite some applications.
So the idea is that you can start an activity and you get information back or databank that you can
then use in the activity that started the other activity.
So the idea is that, for example, when you take an image with your camera and you send the request
from your application, for example, then you want to use the image that you have taken and the image
that you have taken is sent you back as data that you can then use.
So that's basically the idea.
And if, for example, want to get an image from the gallery, then you would start an activity which
will then open up the gallery.
And then when you select an image, it will give you that image back as the result of that activity.
And that's exactly what we have here.
So this is a very basic version of it where you just, for example, launched an activity and then when
you go back, you get a success signal.
So just as OK.
The activity result is success and we're going to define this ourselves.
But then a more interesting one is the second activity.
So once you launch it, you see that here we can enter, for example, the name and email address.
And this will then allow us to get that information on the main activity here.
So we sent this information back, so to speak, to the starter of this activity.
OK, so this is going to be important, as I said, in multiple different cases.
So let's look at how we can build that.
As you could see, we need two activities here, so I'm just going to create them real quick here activity
and this one will be an empty activity.
I'm going to give this one the name first activity and then I create another one.
So let's go ahead and create this second activity here.
Another empty activity and this one will be second activity when it comes to the XML file, the first
activity just has a button and it doesn't really matter where the partners.
What's important is that it has an ID.
And if you look at it well, it's just a button.
In my case in the center, you could have used any type of.
Lay out here, but in this case, it's just a constraint lay out the second activity has to add a text
and a submit button, and in my case, it's again, a constraint layout.
So you could just direct them in there.
What's important is that they have the I.D. that we can then later on use in order to communicate with
them.
And then finally, our main activity just has two buttons and then underneath each button we have a
text you so you can see here I text you and then another button, TextView and button.
Right.
That's pretty much it.
Now, let's go to our main activity, because that's where we then want to start the activities, because
that's where the initial point is our start point, the main activity.
And in here I need a companion object because what I want to do is I want to be able to store my different
variables, my constants, and I want to use them.
So one of them will be our first activity request code and I'm going to assign one to it.
And I'm just going to do it with the first activity for now.
And then later on, we'll see how to do it with the second one.
Now, once I click on the button, so this first activity button that we see.
And our main activity here.
Once I click on this one, what I want to happen is I want to start an activity, of course, so let's
just do that button on the score launch underscore activity first set on Click Listener.
Know my case.
I need to import that button.
But then once somebody clicks on the button, what I want to do is I want to run in intent.
All right.
So let's go ahead and say intent is going to be starting from this activity and it should go over to
the other one.
So it should move over to first activity, colon, colon class, that Java.
And of course, we need to import intent here.
So let's import that real quick.
And then how we would usually start an activity, we will just go ahead and say start activity here.
Now the thing is.
I want to start an activity for assault this time, so I want to also get a result from this activity,
so let's use that one.
So I'm just going to person in a tent and then I will pass and request code.
And the request code is going to be the one that I created here, which is called First Activity Request
Code.
And I'm just assigning a constant here so that I can then later on get back to this specific request
code and we will see why.
Because as soon as we have the on activity result here on activity result, and that's step one here,
once we override that in here, we get a request code result code and data.
All right.
And request code.
Well, you see it here already is the same name.
So we will get a response depending on the request code that we passed once we started an activity for
result.
So the first thing we need to do is we need to make sure that the result code is fine.
So we check here.
If result code is going to be activity, don't result on the score, OK?
Only if that's OK.
We want to check the request code.
So if the request code is going to be the one that we have defined, so does the first.
Activity request code, then we can do whatever we want to do once somebody came back from the first
activity.
So what I'm going to do is I'm just going to set the TV first activity result text to first activity,
result success.
OK, so basically for our main activity here, I said this.
TextView, which is called TV first activity result to be a value, because currently it's just an empty
text view, it doesn't have any text in there.
But once I get back from my activity, which was called first activity, then.
I want to set it.
I want to set the text here.
All right, what should I do if the result code is not OK?
So if something went wrong, for example, the result code is equal to activity dot result canceled.
So what if the user cancelled the activity request?
Well, then I'm just going to write a little toast and add a Lockette, so I'm just going to say canceled
in the lock and then I'm going to write toast, make text result cancelled for a short time.
So now let's go over to the first activity, because from here, what we want to do is, first of all,
give this button that we have.
I functionality, which will just close the activity, but then I also want to set the result so button
on the score, finish if that one is clicked.
So set on Click Listener, then I want to set the result to be activity dot result.
OK.
And then I want to finish the current activity.
OK, so this is really basic, we're just setting the result to be OK, we're not giving it any additional
information, we're just saying, OK, the result was OK, and now let's finish.
All right.
So now let's test this and see what's going to happen.
All right, so let's start the first activity launch, should we get over to this other screen?
Let's finish it and we can see first activity result was a success.
So we said the result to be OK.
And then in the main activity, we checked it.
Was it OK?
Yeah.
If it's OK, then let's check if the request code was the first activity request code and if that's
the case, then set it takes you to be first activity result success.
So basically we just sent the result that we wanted to get, which is very basic, but just said it's
OK.
But what if I actually want to have values from that other screen?
Well, that's where our second activity comes into play because in that one.
But I want to have is additional information.
I want to see what the user has entered in those two text fields which we have here, which are edited
text.
So if we look at it, we have those two edit text here.
And if the user enter something and then press the submit, I want to get this information back to the
main activity.
Well, what I can do therefore is first of all, of course, implement my button.
So button launch activity second set on Click Listener because otherwise of course it's not going to
do anything.
And here I also need an intent similarly to what we have done before.
Pretty much the same approach.
Let's do that real quick here.
The difference is that we want to get to the second activity.
And then what's important is that we have another request code.
So it's not the first activity request code, but it should be the second activity request code.
So I'm going to rename it here.
Second activity request code.
And of course, this doesn't exist yet.
So we need to create that in our companion object here.
Private const file, second activity request code.
And I'm going to assign the value to.
So it's just a constant here, which I assign the value to to.
Now, I'm going to start with just a name, so I'm going to create another constant here, which is
going to be a value called name, and I'm going to assign Disvalue name to it.
OK, so this is the constant variable name and this is the string that I assign to it.
So I just called it name.
All right.
So from the second activity, I want to get back to my main activity, but I also want to send some
information over.
And in order to do so, what I'm going to create is an intent here and an unclick listener.
So Button Submit set on Click Listener.
This pretty much means as soon as somebody clicks on the submit button, I want to do something.
So that's what this does.
And then I want to create an intent.
So I'm just going to create an intent object here.
And this time you can see I don't pass in the activity that I'm currently in.
So no context and I don't pass into which class I want to go.
But in this case, the only thing I do is I want to assign information to this intent so that I can
send it over.
So in this case, I'm just going to say intent put extra and you could see there are many different
overlord's for this function.
So you could put a lot of extra information or different types of information to this intent.
For example, strings, doubles, integers, all the different types of simple data types, so to speak.
OK.
And then you need to give it a name.
So the name hereby is a key.
So we have the key value pair, which pretty much means we have the key, which is where is the information
and then the value, the information itself.
So the key will be my main activity dot name.
So in there I have this variable called name.
This one up here, which is this constant.
And then the value that I want to put extra, so the information that I want to pass over and in my
case, I want to pass over the name of whatever the user has entered.
So I've added text, which means I want to pass on this added text here.
All right, and now what I can do is I can set the result.
To be activity result.
OK, so here is the result.
OK, and I can pass on some more information, which is my intent.
So I'm just going to say intent here and then I can finish the activity.
OK, so this time I'm adding some information that I want to set back or sent back from one activity
to another.
And you can see we said the result and then we sent the intent.
Now, of course, we need to catch that information and the main activity in our own activity result.
So here the last time we checked that the result called here and we checked at the request code, which
was first activity request code.
But this time we're looking at the second activity request code.
So here else if the request code is equal to second activity request code.
Then what I want to do is I want to set the TV second activity result text to be.
And in this case.
It should be, of course, information that I get from the other activities.
So how do I get the information?
So the good thing is when you use on activity result, you get some data here as well.
You can see the result can have data.
So it has a request code to check that one, which is this code that we use ourselves.
And the result code, which is either result, OK, or may be canceled.
And then we get some data as well for some data can be sent.
That is what we have sent here as this put extra data that we pass.
So now we can use that data if it's not null.
So if there is some data, so if it's not now, then please do something with it.
And I want to get the name from the user and I get that from the data that was passed to me.
And here get string extra.
So here I can just get the extra from name.
So why do you use name here?
Well, because.
We passed the information, the other name here, so we used main activity dot name, which is the string,
so which is the key, so to speak, and now we use the same key in order to get the information back.
So it's the same as if we would have used a name here.
Why is that?
Well, because behind name we have the.
String name.
All right.
Now, of course, we can set our second activity results for now, I'm just going to use a name here.
All right, now let's test this and see if it's going to work.
So let's launch the second activity, let's enter your name.
I'm just going to enter Dennis, I'm going to press submit and you can see Dennis was entered here and
that is because we said the result text based on whatever the user has entered.
Now, let me go over here and press back.
You can see result canceled.
So when you don't actually set the button, so when you don't actually set the activity result to be
OK, then you get the canceled event, which is the result code that we have here, result cancel.
All right.
So now you can see that you get the data and now the little challenge for you is to extend that functionality
so that it will not only display the name, but also the email address that the user can enter in this
activity here.
OK, so please go ahead and try to.
All right, I hope you tried it so first of all, I need a new constant so Const. Paul email, it's
going to be email then.
We can go ahead to our second activity and we put extra information, so we put extra and this time
main activity that email and we get the information from it, email, the text to string.
All right, now we put some additional information into the intent that we then set as the result and
then we finished the activity.
So now in our main activity, we can get that information and we get that via, well, email is equal
to data.
Get string extra.
And what's the name of the extra information?
At least a key to it.
What was the email?
And of course, I can go ahead and use that, so here it's going to be name and.
Hema.
All right, now let's test it again.
And it's launched a second activity center name, and I'm just going to enter test and you can see Dennis
and Test both worked.
All right, so this is something that you will need quite often, for example, when you start an intent
for the camera.
So if you want to get something from the camera, when the user takes a picture and you want to set
that into your screen or into your image, you or if you get something from the gallery or in general,
when you start an activity to another, for example, application where you get the data from such as
the gallery and so forth, or if you have an activity which just opens up, such as a pop up, for example,
where the user can enter some data and then disinformation needs to be sent back to another activity
where you can then display the information or work with it.