Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
It is possible to request data from a second activity that will be passed back and used in the original activity. We need to specify this when we start the activity by using a different method called startActivityForResult(), and then we can return data to the original Activity in a few different ways.
Sometimes we start a second activity with
the intention of getting some data to use
0:00
in the original activity.
0:04
We need to specify this when we start
the activity by using a different method,
0:05
start activity for result.
0:09
So back here., let's delete this line
where we are starting the activity,
0:12
and instead call it
startActivityForResult.
0:15
There we go.
0:19
The first parameter like
before is our intent, but
0:21
now we also need to
include a request code.
0:25
Like with extras, it's a good practice
to define some reasonable constants.
0:28
So let's call this REQUEST_FAVORITE and
then define it at the top of our class.
0:31
So I'm going to scroll back up to the top
in here where we have our static strings.
0:40
Let's do public static final and
in this case it's an int or
0:44
request code has to be an int and
we called it REQUEST_FAVORITE and
0:48
we can set this to really anything
we want let's just set it to zero.
0:52
We'll just count up from
different requests values
0:56
are useful when we request different
types of information from an activity.
0:59
All right so now we're expecting a result
to come back here to main activity.
1:03
How will we handle it?
1:07
Using start activity for result requires
that we implement another method
1:09
in the activity to handle the result.
1:13
Let's scroll down all the way to
the bottom of our class and have it.
1:15
I'm gonna add a few lines here just to
keep my code up higher in the screen, and
1:18
now let's type onActivityResult and
hit Enter, and
1:23
studio will drop in
the method signature for us.
1:27
Check out the parameters of this method,
the first is the request code,
1:30
if we have multiple types of request but
always come back to this method.
1:34
So we can check this request code of
each to handle them in different ways.
1:37
We only have one requestCode,
but still it's at a check,
1:43
if requestCode equals and
then we call that REQUEST_FAVORITE.
1:47
The second parameter is a resultCode,
1:53
we are always going to want to
inspect the resultCode as well.
1:56
So let's add, If resultCode equals,
and then start typing, in all caps,
1:59
RESULT_ and the activity class
has some predefined result codes,
2:05
CANCELED, FIRST_USER, and OK.
2:09
You can check the activity API for
more info on these.
2:12
RESULT_CANCELED is returned when the user
hits the back button or we can trigger it
2:15
ourselves, we'll use RESULT_OK, so
let's select that and add a curly braces.
2:18
All right let's follow the flow of
control over in DetailActivity.
2:23
So I'm going to open that up and
now we're starting this activity and
2:28
expecting a result.
2:32
So we need to create one,
2:34
let's do that when the user
taps on the favorite checkbox.
2:35
So in here, create bottom
we're gonna add some lines and
2:38
attach an oncheckedchanged listener.
2:42
So we type,
favoriteCheckbox.setOnCheckedChangeList-
2:45
ener and we wanna create a new one
just like we do for onclick listeners.
2:50
So a new OnCheckedChanged listener,
2:54
in here is the method that gets called and
in here we need another intent
2:56
to hold the data that we want to
transfer back to the calling activity.
2:59
So let's make one Intent, let's call
this result Intent = a new Intent.
3:03
Let's add the value of this checkbox as
a boolean extra, resultIntent.put[Extra].
3:09
And for the name, let's add another
static constant in main activity or
3:16
call it extra favorite.
3:20
We'll define it in a moment but here
we can use MainActivity.EXTRA_FAVORITE.
3:21
Now this method passes in
a boolean is checked that will be
3:30
true if the value is checked.
3:33
So let's use that.
3:34
And now we want to set the result
using the activities setResult method.
3:36
We'll use like we said RESULT_OK.
3:41
But we also want to attach this intent,
resultIntent, as a second parameter for
3:44
this method.
3:49
To finish this activity and
3:51
return to the main one,
let's call the activities finish method.
3:52
Now back in mainActivity let's go up and
add that constant we created.
3:57
Public static final String and
we called it EXTRA_FAVORITE and
4:01
let's set it equal to EXTRA_FAVORITE.
4:08
Now we need to do something with the
result down in our method at the bottom.
4:14
So back here where we have
our if statements, for
4:19
the moment let's just log the result.
4:22
So we'll set a boolean variable.
4:24
Boolean result equals,
4:26
now knows that the intent that's returned
is returned as a parameter called data so
4:28
we can access it with
data.getBooleanExtra and
4:33
we have the name EXTRA_FAVORITE.
4:38
And let's log it with Log.i.
4:42
We have a tag we can use
that will say "Is favorite?
4:44
" and will append the result Boolean.
4:48
And let's check it out.
4:52
Whoops, I notice that
I have an error here.
4:55
So this is happening because
the getBooleanExtra method
4:57
requires a default value just in
case the Boolean isn't present.
5:00
So we usually want to set
false as our default value.
5:04
We also have an error in detail
activities, let's go take a look.
5:07
And there, now it's resolved, it just need
to pick up the change from main activity.
5:11
So now let's run this to see how it works.
5:14
Okay, so first we need to tap
on the download button, and
5:18
we come to the detail page,
we check the favorite box
5:22
And it's gonna go back
to main activity and
5:28
once it does I saw in logcat that the log
entry we added already showed up.
5:31
So if I scroll up a little bit,
I can see here, is favorite?equals true.
5:36
All right round trip
communication complete.
5:41
You need to sign up for Treehouse in order to download course files.
Sign up