Sending Data to a New Activity2:53 with Ben Jakuben
Intents can also carry data! In this video we will see how to put "extra" data in an Intent that can be retrieved by whatever receives the Intent.
We have the user's name here in main activity but it's stuck in there. 0:00 We can't access it yet in our new story activity. 0:03 If you are relatively new to programming you might think we can add some sort of 0:06 member variable up here and share it somehow. 0:10 But that would create a type coupling between two activities that 0:12 should not exist. 0:14 Such type integrations like that cause problems down the road 0:16 as systems grow in complexity. 0:18 Our best solution is to pass the data along somehow. 0:20 This functionality is provided in the intent object we are using to start 0:23 the new activity. 0:26 The actual intent object gets passed to the new activity. 0:28 So if we attach data to it, we can then access that data in the activity, 0:31 where the intent ends up. 0:35 Intents can hold all sorts of data, called extras. 0:36 We put them into the intent using one of its many put methods. 0:39 Here, let's try it. 0:42 Let's go back to our new start story method. 0:43 And before we call start activity, let's add a new line and 0:46 type intent dot and start typing put, 0:49 and we can see all of the different methods here available to us. 0:51 We attach data to an intent as a key value pair which is very common in programming. 0:55 The key is a string name so you can see that as the first parameter here, and 1:00 we use that name to reference the data that we attach as the second parameter. 1:03 So with that key we can then pull out the data in the other activity. 1:07 We're going to pass in the name variable which is a string, so 1:11 we want the put extra that takes a string key and attaches a string value like this. 1:13 You don't have to select that one, 1:19 it will get selected automatically based on the parameters we pass in. 1:20 For the key, let's call this name. 1:24 And then for the value, we can use the name variable, but wait, 1:27 our name variable is out of scope. 1:30 Now, it's not giving us an error here, but 1:32 we need to pass in the name that we're setting up here in the onClick method. 1:34 You're hopefully familiar with variable scope from Java basics or 1:38 other programming. 1:41 But, if not, you need to know that variables only exist 1:42 within the blocks of code where they are defined. 1:45 That's the scope of the variable. 1:47 So because the name variable is declared up here inside the onClick method, 1:49 it's only available in these lines of code here unless we pass it out somewhere else. 1:53 So to make it available in this new method down here, this new block of code, 1:58 we need to either pass it as a parameter to the startStory method, or we could make 2:02 it a class level member variable that's available anywhere inside this class. 2:06 That's fine, but it's usually better to constrain the scope where we need it. 2:11 So let's pass it as a parameter. 2:15 Member variables should ideally be the properties of the object and 2:17 the name here isn't really a property of this activity. 2:20 It's going to be part of our story and we are just passing it along via the intent. 2:22 So let's add a new parameter here. 2:26 In our definition we'll say, String name, 2:29 and now we can add the name variable here in the parentheses above. 2:32 If you're wondering why we can use the same name for the variable here and 2:36 in the parameter down here, that's again because of the scope. 2:39 These are two totally different scopes so 2:43 the variables have nothing to do with each other. 2:45 They can thus be named the same because once we're out of this method up here, 2:47 this variable will cease to exist. 2:51
You need to sign up for Treehouse in order to download course files.Sign up