Intent Extras and Results4:28 with Ben Jakuben
Extra data that is required for an Intent is added as an "extra." We can pass all sorts of information along with an Intent this way, which is available in the target Activity that handles the Intent.
Let's continue with an example of simple data. 0:00 We'll pass a song title to the detail activity and 0:02 then we'll pass back whether or not that song is a favorite for the user. 0:05 When we start an activity, 0:10 this intent object is actually passed to the new activity. 0:11 Because of this fact, we can add things to the intent and 0:14 use it as a little delivery person to carry data somewhere else. 0:17 Pretty neat, right? 0:20 Extra data like this is called, well, extras. 0:22 I love it when names make sense. 0:25 Let's add a new line here after we declare our intent and type intent.putExtra. 0:26 Now take a look at this. 0:33 This is a long list of different data types we can add as extras. 0:34 We can add lots of extras if we want, we just need to give each a unique name. 0:39 That's the first parameter you see in each of these methods' signatures. 0:43 We want to pass in some string data, so let's give it a name called SONG_TITLE, 0:47 in all caps, and then we can pass in the actual title. 0:52 I'll use my favorite Android song Gradle, Gradle, Gradle. 0:55 Now let's go over to the detail activity and see how to unpack this extra data. 1:01 We'll start by getting the intent used to start this activity. 1:06 So here at the bottom of our uncreate method, let's add a few lines and type 1:10 Intent intent = and then we can call a method from the activity class getIntent. 1:14 This gets us the specific intent that was used to start this activity. 1:21 Next, let's unpack the title into a string named songTitle. 1:27 The intent class includes get methods for each data type. 1:32 So, in our case we can call intent.getStringExtra and 1:35 pass in that same name, SONG_TITLE. 1:40 Now your code smell sense should be tingling right now. 1:44 We are repeating ourselves with this magic key value. 1:47 Let's convert it to something more useful. 1:50 Back in MainActivity, let's add at the top a new static constant. 1:53 So here I'm going to type public static final sString, and 1:58 let's call this EXTRA_TITLE. 2:03 Let's set it equal to the same value EXTRA_TITLE. 2:06 We can set it to whatever value we want, but I like to make mine match the keys. 2:09 Okay, so let's go down to the code in our testIntents method and 2:15 we can get rid of SONG_TITLE and just use EXTRA_TITLE. 2:18 And back over in DetailActivity we can make the same change. 2:24 Get rid of SONG_TITLE, and we need to preface it with MainActivity.EXTRA_TITLE. 2:26 There, that's better. 2:33 So with that we can use this value to update our title text view. 2:34 So it's called titleLabel.setText and then I'm gonna pass in songTitle. 2:37 A quick question. 2:45 What if we forgot to attach this extra to our intent? 2:47 We'd end up with a null value here. 2:50 We can't guarantee that this activity is lodged with an intent 2:52 that includes this specific extra piece of data. 2:56 That's the power of intents. 2:59 We can use them in any app, 3:00 we don't always need to start an activity the exact same way. 3:01 To protect against this, we need to add a simple if statement to check for 3:06 null from the intent before trying to do anything with it. 3:09 So we say, if intent.getStringExtra, 3:12 same thing, we want to use MainActivity.EXTRA_TITLE. 3:17 If it does not equal null, then we want to get it and set the value. 3:22 So let's cut this line, paste it in here, and 3:27 do the same thing for setting the text view. 3:32 All right, so we're all set. 3:36 Let's verify by running the app. 3:37 Okay so if we click on the download button, then we should see the title. 3:43 There it is, Gradle, Gradle, Gradle. 3:48 All right, looking good. 3:49 By the way, this same pattern applies to sending data to services too. 3:51 In fact, we already have that in this code from the services course. 3:55 Check out the downloadSongs method back in main activity. 3:59 This creates an explicit intent, but 4:04 it passes the class name of a service, not an activity. 4:06 We putExtra data in it, just like with activities. 4:10 And then we call startService, instead of startActivity. 4:13 Remember this as we go forward, because everything we see for 4:17 activities applies equally to services. 4:19 We'll also revisit this later in the course. 4:21 Let's take a short break and 4:24 then we'll look at passing data in the reverse direction. 4:25
You need to sign up for Treehouse in order to download course files.Sign up