This course will be retired on July 14, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Intents and Broadcast Receivers!
You have completed Intents and Broadcast Receivers!
Preview
After seeing how to pack up data as a Parcelable object, let's revisit how to unpack it as an Extra on an Intent used to start a new Activity.
Helpful Links
- Android Lists and Adapters - We have a stage in this course called "Using Parcelable Data". Check it out or review it if you want to brush up on how to implement the Parcelable interface.
- Benefit of using Parcelable instead of serializing object
- parcelabler - A tool for generating some Parcelable code for existing objects.
Documentation
Downloads
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
All right, let's switch to DetailActivity
and handle the data on that side.
0:00
Let's comment out these few lines
here because we aren't going to
0:05
use them anymore.
0:07
I'll just leave them here as reference.
0:08
So we still want to use a null check but
0:11
this time we're going to call
a different intent method.
0:13
If intent.get, and here we want
to use the getParcelableExtra,
0:15
since we're passing in parcelable data.
0:21
We passed in with MainActivity.EXTRA_SONG.
0:24
And we want to make sure that
this is not equal to null.
0:28
Okay, so now inside here,
it's not null, so
0:33
we can use that same line to
initialize a song variable.
0:36
Song song = and
I'm going to copy and paste.
0:39
And here we go,
now we have a song variable.
0:45
Now we can set the title
from this variable.
0:49
So titleLabel.setText, and
we can use song.getTitle, and we can also
0:50
initialize our favorite check box
because that is part of the song object.
0:55
So we can say favoriteCheckbox.setChecked
and we can use song.isFavorite.
1:00
We need one more thing.
1:07
Remember that we are tracking the position
of the song that was tapped on.
1:08
We can get it as an int ListPosition.
1:12
I'm actually gonna put that
outside this if statement.
1:13
So, int ListPosition = intent.getIntExtra.
1:16
And the name was
MainActivity.EXTRA_LIST_POSITION.
1:22
And the getInt method
requires a default value.
1:27
This is why we don't need to
put it inside of a check.
1:30
So if it's not there, we'll just use
a default and we should use 0 for a list.
1:31
To use this list position
back in MainActivity,
1:37
we want to pass it back as
a result in our result intent.
1:39
So here where we're putting extra data,
1:43
let's set on our line
result intent.putExtra.
1:45
And we'll use the same key,
MainActivity.EXTRA_LIST_POSITION, and
1:48
we'll pass in listPosition.
1:51
Notice that Android Studio automatically
added the final keyword, which we need
1:55
when we're referencing the variable inside
of an anonymous class like we are here.
1:59
The last piece of this puzzle
is back in MainActivity.
2:04
We want to show the favorite
heart if the song is checked.
2:07
So now instead of just logging
the boolean result down here,
2:10
let's use it to update the view.
2:13
First we need the position in the list int
position = and our intent is called data.
2:15
And like before, we can call getIntExtra,
2:20
pass in EXTRA_LIST_POSITION and
a default value of 0.
2:23
Now, how can we update an individual
view in a separate recycler view?
2:27
The solution is to update the underlying
data and then notify the adapter that
2:33
the data has been changed, and that the
item should be bound to the view again.
2:37
We can update the data
using the Playlist class,
2:41
Playlist.songs, and this is an array,
so we can pass in the position,
2:44
and then we can set the favorite
value with setIsFavorite.
2:48
And we have the value stored in result.
2:53
To notify the adapter of
a change at a specific position,
2:57
we'll call mAdapter.notifyItemChanged
at a certain position.
3:00
Now we can run this to see it in action.
3:07
So first we need to tap
on a song in the list.
3:13
I'll tap on this one, Live and
Die, and it's going to open up.
3:15
I'm going to check it as a favorite.
3:18
And when we return to the MainActivity,
there we go,
3:22
we see our favorite heart icon.
3:25
Now if we go back in,
let's tap on Live and Die again.
3:29
We'll uncheck the checkbox,
and we'll return.
3:33
And the heart icon disappears.
3:38
Let's take another look at that table
of different types of communication.
3:41
We've fully covered how to pass both
simple and complex data back and
3:45
forth between activities,
using intents and extras.
3:48
Well, it still looks like
we have a lot to cover.
3:52
Understanding this foundation makes
the rest of these pretty easy.
3:54
It's the same basic idea over and over.
3:58
Share information as extra
data on an intent object, and
4:01
then make sure that extra
data is handled somehow.
4:04
Remember, everything we've covered for
activities works the same way for
4:08
communicating to and from services too.
4:11
Nice work so far.
4:15
I now intend to take a short break,
and I'll make it explicit
4:16
by naming the activity that will
handle my intention, playing a game.
4:20
You take a break, too, but come back soon,
4:24
because I wanna get back to those
implicit intents we touched on earlier.
4:25
In fact, if you'd like,
4:30
why don't you try creating an implicit
intent to share location on a map?
4:32
I'll leave a link in the teacher's
notes and you can try it out, and
4:36
then we can look at it
together in the next section.
4:39
Be sure to watch even
if you get it working,
4:41
because I want to show
you some special cases.
4:43
Good luck.
4:46
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up