1 00:00:00,700 --> 00:00:02,600 Let's continue with an example of simple data. 2 00:00:02,600 --> 00:00:05,640 We'll pass a song title to the detail activity and 3 00:00:05,640 --> 00:00:08,560 then we'll pass back whether or not that song is a favorite for the user. 4 00:00:10,070 --> 00:00:11,470 When we start an activity, 5 00:00:11,470 --> 00:00:14,690 this intent object is actually passed to the new activity. 6 00:00:14,690 --> 00:00:17,030 Because of this fact, we can add things to the intent and 7 00:00:17,030 --> 00:00:20,410 use it as a little delivery person to carry data somewhere else. 8 00:00:20,410 --> 00:00:22,050 Pretty neat, right? 9 00:00:22,050 --> 00:00:25,140 Extra data like this is called, well, extras. 10 00:00:25,140 --> 00:00:26,920 I love it when names make sense. 11 00:00:26,920 --> 00:00:33,571 Let's add a new line here after we declare our intent and type intent.putExtra. 12 00:00:33,571 --> 00:00:34,790 Now take a look at this. 13 00:00:34,790 --> 00:00:39,340 This is a long list of different data types we can add as extras. 14 00:00:39,340 --> 00:00:43,780 We can add lots of extras if we want, we just need to give each a unique name. 15 00:00:43,780 --> 00:00:46,610 That's the first parameter you see in each of these methods' signatures. 16 00:00:47,670 --> 00:00:52,500 We want to pass in some string data, so let's give it a name called SONG_TITLE, 17 00:00:52,500 --> 00:00:55,490 in all caps, and then we can pass in the actual title. 18 00:00:55,490 --> 00:00:59,450 I'll use my favorite Android song Gradle, Gradle, Gradle. 19 00:01:01,800 --> 00:01:06,940 Now let's go over to the detail activity and see how to unpack this extra data. 20 00:01:06,940 --> 00:01:10,270 We'll start by getting the intent used to start this activity. 21 00:01:10,270 --> 00:01:14,034 So here at the bottom of our uncreate method, let's add a few lines and type 22 00:01:14,034 --> 00:01:19,530 Intent intent = and then we can call a method from the activity class getIntent. 23 00:01:21,750 --> 00:01:25,730 This gets us the specific intent that was used to start this activity. 24 00:01:27,470 --> 00:01:31,675 Next, let's unpack the title into a string named songTitle. 25 00:01:32,790 --> 00:01:35,770 The intent class includes get methods for each data type. 26 00:01:35,770 --> 00:01:40,500 So, in our case we can call intent.getStringExtra and 27 00:01:40,500 --> 00:01:42,785 pass in that same name, SONG_TITLE. 28 00:01:44,570 --> 00:01:47,500 Now your code smell sense should be tingling right now. 29 00:01:47,500 --> 00:01:50,510 We are repeating ourselves with this magic key value. 30 00:01:50,510 --> 00:01:52,000 Let's convert it to something more useful. 31 00:01:53,500 --> 00:01:57,880 Back in MainActivity, let's add at the top a new static constant. 32 00:01:58,970 --> 00:02:03,396 So here I'm going to type public static final sString, and 33 00:02:03,396 --> 00:02:05,884 let's call this EXTRA_TITLE. 34 00:02:06,950 --> 00:02:09,670 Let's set it equal to the same value EXTRA_TITLE. 35 00:02:09,670 --> 00:02:13,070 We can set it to whatever value we want, but I like to make mine match the keys. 36 00:02:15,165 --> 00:02:18,890 Okay, so let's go down to the code in our testIntents method and 37 00:02:18,890 --> 00:02:24,210 we can get rid of SONG_TITLE and just use EXTRA_TITLE. 38 00:02:24,210 --> 00:02:26,990 And back over in DetailActivity we can make the same change. 39 00:02:26,990 --> 00:02:33,346 Get rid of SONG_TITLE, and we need to preface it with MainActivity.EXTRA_TITLE. 40 00:02:33,346 --> 00:02:34,561 There, that's better. 41 00:02:34,561 --> 00:02:37,970 So with that we can use this value to update our title text view. 42 00:02:37,970 --> 00:02:43,490 So it's called titleLabel.setText and then I'm gonna pass in songTitle. 43 00:02:45,950 --> 00:02:47,080 A quick question. 44 00:02:47,080 --> 00:02:50,510 What if we forgot to attach this extra to our intent? 45 00:02:50,510 --> 00:02:52,790 We'd end up with a null value here. 46 00:02:52,790 --> 00:02:56,060 We can't guarantee that this activity is lodged with an intent 47 00:02:56,060 --> 00:02:59,180 that includes this specific extra piece of data. 48 00:02:59,180 --> 00:03:00,520 That's the power of intents. 49 00:03:00,520 --> 00:03:01,910 We can use them in any app, 50 00:03:01,910 --> 00:03:04,950 we don't always need to start an activity the exact same way. 51 00:03:06,060 --> 00:03:09,500 To protect against this, we need to add a simple if statement to check for 52 00:03:09,500 --> 00:03:12,720 null from the intent before trying to do anything with it. 53 00:03:12,720 --> 00:03:17,980 So we say, if intent.getStringExtra, 54 00:03:17,980 --> 00:03:22,770 same thing, we want to use MainActivity.EXTRA_TITLE. 55 00:03:22,770 --> 00:03:27,030 If it does not equal null, then we want to get it and set the value. 56 00:03:27,030 --> 00:03:32,310 So let's cut this line, paste it in here, and 57 00:03:32,310 --> 00:03:34,980 do the same thing for setting the text view. 58 00:03:36,600 --> 00:03:37,780 All right, so we're all set. 59 00:03:37,780 --> 00:03:39,085 Let's verify by running the app. 60 00:03:43,035 --> 00:03:48,320 Okay so if we click on the download button, then we should see the title. 61 00:03:48,320 --> 00:03:49,960 There it is, Gradle, Gradle, Gradle. 62 00:03:49,960 --> 00:03:51,370 All right, looking good. 63 00:03:51,370 --> 00:03:55,620 By the way, this same pattern applies to sending data to services too. 64 00:03:55,620 --> 00:03:59,420 In fact, we already have that in this code from the services course. 65 00:03:59,420 --> 00:04:02,020 Check out the downloadSongs method back in main activity. 66 00:04:04,570 --> 00:04:06,770 This creates an explicit intent, but 67 00:04:06,770 --> 00:04:10,822 it passes the class name of a service, not an activity. 68 00:04:10,822 --> 00:04:13,140 We putExtra data in it, just like with activities. 69 00:04:13,140 --> 00:04:17,030 And then we call startService, instead of startActivity. 70 00:04:17,030 --> 00:04:19,250 Remember this as we go forward, because everything we see for 71 00:04:19,250 --> 00:04:21,950 activities applies equally to services. 72 00:04:21,950 --> 00:04:24,410 We'll also revisit this later in the course. 73 00:04:24,410 --> 00:04:25,450 Let's take a short break and 74 00:04:25,450 --> 00:04:27,570 then we'll look at passing data in the reverse direction.