Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Getting Data from an Intent3:06 with Ben Jakuben
Our second Activity is being started by an Intent. We can access the Intent used to start it and retrieve the "extra" data that was added to it.
Our intent object now has some data, cool. 0:00 But what do we do on the other side? 0:03 What code do we need to add in the activity where we are sending this intent? 0:05 So this new activity will be started with the intent, so 0:09 we can access it right away inside the onCreate method. 0:12 Add a few lines under setContentView. 0:15 And then here we are going to get the intent used to start this activity, so 0:17 let's declare an intent variable named intent. 0:20 The activity base class, as usual, has a helpful method for us, so type = and 0:23 start typing get, and look at that, from auto complete we have getIntent. 0:29 All right, now what exactly does this do? 0:33 Well, let's check the documentation. 0:35 We can click on the method and 0:37 then show documentation with either F1 or Ctrl+J on a Mac. 0:39 Or Ctrl+Q on Windows. 0:43 I know that's kind of a mouthful of keyboard shortcut combos, 0:45 but this is a useful one that it pays to know. 0:48 Anyhow, it says here that this returns the intent used to start the activity. 0:50 Perfect. 0:55 But now what do we do with it? 0:56 Well, let's gamble and see if we can figure it out from autocomplete. 0:57 We can explore this intent on a new line. 1:01 Type intent., and then here, let's see, what's the opposite of put extra? 1:03 We want to get data out of it, so let's start typing get, and look at that. 1:08 There's a bunch of get certain data types of extras here, so we want getStringExtra. 1:12 Let's see, getStringExtra, and there we go, it takes a string name as a parameter, 1:17 which is the key for the string value we want. 1:23 But we need to pass in the key that we used, which was name in all underscores. 1:26 Let's peek at the documentation again just to make sure. 1:31 If we hit Ctrl+J on my Mac, then this tells us it gets a string, 1:34 it returns a string, based on the key name. 1:39 Cool, just what we want. 1:42 All right, we have enough to test this out. 1:43 So let's use a log statement to make sure that this new name is available 1:44 in the activity. 1:48 log.d, and we want to create a tag. 1:49 We'll do that in a second, and we'll pass in, we need to save this in a variable. 1:52 So let's call this String name = And then we can pass in name as the log message. 1:56 And for tag, let's create it up here. 2:04 Type public static final string, TAG in all uppercase, and 2:05 we'll set it equal to the name of the activity, StoryActivity. 2:11 But we'll do it, again, with that shortcut. 2:17 StoryActivity.class.getSimpleName. 2:19 So that if we ever change the name, it will be refactored for us automatically. 2:23 Let's run this and make sure that it's working. 2:27 Okay, so once again, I will type my name, tap on the button, and 2:30 if we go to the log, bring it up here, we have a debug statement, let's look for it. 2:34 Yes, there we go. 2:40 We've got debug inside of our story activity and there's my name. 2:41 All right, mission accomplished. 2:47 But we have a fragile spot in our code. 2:49 We're using the same key in two different places. 2:51 One to put our extra into our intent, and one to get it out. 2:55 What happens if we type it slightly differently? 2:58 Here, let's change this to a capital N, and in the next video, 3:00 we'll explore what this does, and how to fix it. 3:03
You need to sign up for Treehouse in order to download course files.Sign up