Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Let's see how to send complex data from one app to another. We'll need to create a custom intent and a custom intent filter in a second app.
-
0:00
We've already seen how to bundle up a song and
-
0:02
send it by an intent from one activity to another.
-
0:05
We'll do the same thing here to send it from one app to another but
-
0:08
we need to create a custom intent and a custom intent filter in a second app.
-
0:14
Let's do this from detail activity, where we already have a specific song chosen.
-
0:19
I already created a menu option for us.
-
0:21
So, all we need to do is define our new intent and
-
0:23
share it when that menu option is tapped.
-
0:25
First we need a song though.
-
0:27
Right now it's only available here in the onCreate method
-
0:30
because that's the only place we are using it.
-
0:33
I've also already seated these starter files with a member variable mSong.
-
0:37
So let's just switch that.
-
0:39
So we'll change song to mSong, and we'll do the same thing down here,
-
0:44
mSong, and over here, msong.
-
0:47
Okay, now let's scroll down to the methods for the menu.
-
0:51
First is the create method, and then we have the onOptionsItemSelected,
-
0:56
which is called when a menu item is Selected.
-
1:00
Now it's good practice to check the item id and then perform actions on it,
-
1:04
we only have one item id but let's follow the best practice and check it, so
-
1:08
if we expand menu and look at the detail XML, we see that the id is action_ share.
-
1:15
Let's go back here at a test, if ItemID = r.id.action_share.
-
1:23
Then we want to share the data.
-
1:26
Let's play it safe in here and add a check to make sure that our song isn't null so
-
1:30
if Msong does not equal null then we will continue.
-
1:36
Let's create a custom intent CustomIntent equals a new Intent.
-
1:43
What we really need is a custom action.
-
1:46
It's just a string value, but we want it to be unique so that it doesn't collide
-
1:50
with any other custom actions that any other developers might have made up.
-
1:54
Common practice when we need to use unique values
-
1:57
is to prefix them with the package name.
-
1:59
So let's create a constant to use down here.
-
2:01
Up at the top let's add public static final string.
-
2:06
And let's call this share_song.
-
2:12
Let's set this equal to the Packers name, come.teamtreehouse.
-
2:17
And then I'll follow the intent.ActionPattern used by Android.
-
2:22
We'll finish with .SHARE_SONG.
-
2:25
Let's go back down and use this in our constructor.
-
2:33
So now we pass in SHARE_SONG.
-
2:36
And now we can add the song as an extra since it's parsable.
-
2:39
customIntent.putExtra(mSong).
-
2:44
Whoops I forgot to pass in a key.
-
2:47
Let's add that.
-
2:48
We'll use the same one as before, MainActivity.EXTRA_SONG, mSong.
-
2:52
There we go.
-
2:54
Finally lets start the activity.
-
2:57
But rather than just calling regular old startActivity
-
3:00
let's follow another best practice and use an activity chooser.
-
3:04
Let's revisit our handy intense and filters page.
-
3:07
And now we wanna look at Forcing an app chooser.
-
3:11
The app chooser is a nice feature because it allows the user to pick a specific app
-
3:15
and even make that app the default for that action for
-
3:17
given app We just need to create a second intent to create a chooser for
-
3:22
the actual intent, which we see down here.
-
3:26
So before we call this, let's create a second intent called intent
-
3:30
chooser equals intent.createchooser.
-
3:36
Then we pass in our custom intent and a title for the chooser dialog.
-
3:40
Let's say Share song, and now we can pass in chooser to start activity.
-
3:48
Now we can't try this yet because we don't have anything that can receive it, but
-
3:51
still let's take a look.
-
3:57
So let's pick one of the songs in our list and.
-
4:01
We got an error.
-
4:02
Let's take a look and see.
-
4:04
So, here in the stack trace we see that it is a NullPointerException and
-
4:09
it links to the offending line and I see what it is.
-
4:13
This is a common mistake I made it before.
-
4:16
We want to call the .equals method and
-
4:19
the Intent.ACTION said I want to switch it around the order.
-
4:22
Now right now get action might be N o and it's giving us a No pointer.
-
4:26
So instead we say intent.
-
4:28
ACTION_SEND.equals And then we pass in intent.getAction.
-
4:36
There that's better let's try this again.
-
4:40
So, now if we click on a song we're taken to the detail view.
-
4:45
And if we try the menu we can click on share song.
-
4:50
But now it's hard to see it says no apps can perform this action.
-
4:54
One of the good things about using a chooser like this is that we avoid
-
4:57
a runtime exception.
-
4:58
If we weren't using a chooser we would need to add another check like we did
-
5:01
before by calling result activity and checking the package manager for handlers.
You need to sign up for Treehouse in order to download course files.
Sign up