1 00:00:00,830 --> 00:00:04,110 Let's start with one of the most common scenarios we encounter in Android, 2 00:00:04,110 --> 00:00:06,760 passing data from one activity to another. 3 00:00:06,760 --> 00:00:09,750 If you've been taking other courses here, then you've already seen this, of course. 4 00:00:09,750 --> 00:00:12,730 But let's take a deeper look at what Android is doing for us, so 5 00:00:12,730 --> 00:00:16,530 we can better understand how intents work and the different ways we can use them. 6 00:00:17,840 --> 00:00:18,540 A quick note. 7 00:00:18,540 --> 00:00:21,930 I forgot to include a new app icon in the initial project files. 8 00:00:21,930 --> 00:00:26,707 If you want to use it, download it from the teacher's notes, delete the default 9 00:00:26,707 --> 00:00:31,293 icons here in the res > mipmap folder, and then put the new icon file in here. 10 00:00:31,293 --> 00:00:33,980 All right, so let's close this build.gradle file and 11 00:00:33,980 --> 00:00:35,740 open up MainActivity. 12 00:00:35,740 --> 00:00:39,450 So expand Java and then the package and double click on it. 13 00:00:41,960 --> 00:00:45,958 Now for this brief exercise, let's hijack the code in our download button. 14 00:00:45,958 --> 00:00:48,460 So I'm gonna scroll down a little bit and 15 00:00:48,460 --> 00:00:53,478 we have an OnClickListener here in the OnCreate method for our download button. 16 00:00:53,478 --> 00:00:55,969 I'll start by refactoring this code into a method. 17 00:00:55,969 --> 00:01:00,964 So I'm going to select everything, right click on it, 18 00:01:00,964 --> 00:01:04,594 then refactor, Extract into a Method. 19 00:01:04,594 --> 00:01:08,557 Make sure we select MainActivity, not an anonymous method. 20 00:01:08,557 --> 00:01:14,553 And let's call this downloadSongs. 21 00:01:14,553 --> 00:01:16,090 Hit Enter. 22 00:01:16,090 --> 00:01:19,090 And okay, so now it's down at the bottom. 23 00:01:19,090 --> 00:01:19,960 Our code was collapsed. 24 00:01:19,960 --> 00:01:23,720 I'll just click on it to expand it and a space and 25 00:01:23,720 --> 00:01:26,020 I'm going to comment this code out for now. 26 00:01:27,980 --> 00:01:32,405 Let's create a new method in here to simply test intents. 27 00:01:34,962 --> 00:01:39,019 Now if we go back to this method and use Alt+Enter, our quick fix, 28 00:01:39,019 --> 00:01:40,910 we can create this new method. 29 00:01:42,060 --> 00:01:44,430 And once again select MainActivity. 30 00:01:44,430 --> 00:01:45,850 And here we go we have a new method stub. 31 00:01:47,260 --> 00:01:50,750 In here all we want to do is create a new intent. 32 00:01:50,750 --> 00:01:51,880 Let's call it intent. 33 00:01:53,430 --> 00:01:57,570 And then we'll use the constructor new Intent that expresses our intent to 34 00:01:57,570 --> 00:02:02,160 start a new activity which needs the current contexts as the first parameter. 35 00:02:02,160 --> 00:02:09,194 And the name of the target activity as the second parameter, DetailActivity.class. 36 00:02:11,440 --> 00:02:15,660 Now to start this activity, we call startActivity and pass in this new intent. 37 00:02:17,920 --> 00:02:20,313 So let's try this out just to make sure that it's working. 38 00:02:24,325 --> 00:02:27,150 Okay, here's app and since we hijacked the download button. 39 00:02:27,150 --> 00:02:29,207 Let's tap on that. 40 00:02:29,207 --> 00:02:31,780 All right, and here's our second activity. 41 00:02:31,780 --> 00:02:33,170 So let's take another look at this code. 42 00:02:35,050 --> 00:02:39,410 This intent, where we name the class that's going to handle it, 43 00:02:39,410 --> 00:02:43,820 is known as an explicit intent. 44 00:02:46,320 --> 00:02:49,170 Let's take a quick look at some of the documentation on the Android Developer's 45 00:02:49,170 --> 00:02:50,910 site about intents. 46 00:02:50,910 --> 00:02:54,951 If we scroll down just a little bit, we see a section here on intent types. 47 00:02:55,980 --> 00:03:00,540 There are two types of intents, explicit intents and implicit intents. 48 00:03:00,540 --> 00:03:05,140 So here in explicit, note that it says you'll typically use an explicit intent to 49 00:03:05,140 --> 00:03:09,410 start a component in your own app because you know the class name of the activity or 50 00:03:09,410 --> 00:03:10,379 service you want to start. 51 00:03:11,770 --> 00:03:13,000 So right back here, 52 00:03:13,000 --> 00:03:16,600 we are explicitly using the class name of the target activity. 53 00:03:17,630 --> 00:03:20,950 So what do you think it means to use an implicit intent? 54 00:03:20,950 --> 00:03:25,790 What intent would we imply without explicitly using a known class? 55 00:03:25,790 --> 00:03:29,980 This is where the true power and beauty of intents comes into play. 56 00:03:29,980 --> 00:03:31,940 This is something I love about Android. 57 00:03:31,940 --> 00:03:35,250 We can use intents to easily leverage other parts of Android, or 58 00:03:35,250 --> 00:03:39,418 other apps, to help us do things in the context of our own app. 59 00:03:39,418 --> 00:03:43,130 Intents change our device from a grab bag of independent apps 60 00:03:43,130 --> 00:03:45,010 to a system of integrated components. 61 00:03:46,040 --> 00:03:47,730 What I really mean is this. 62 00:03:47,730 --> 00:03:52,330 Implicit intents allow us to imply a solution to handling an intent 63 00:03:52,330 --> 00:03:56,080 without explicitly defining what is going to handle that intent. 64 00:03:56,080 --> 00:03:59,320 In other words, we say we wanna do something without knowing exactly 65 00:03:59,320 --> 00:04:03,930 how to do it, but trusting the Android system to help us figure it out. 66 00:04:03,930 --> 00:04:08,260 Or if we take a look at the definition in the docs, we declare a general action to 67 00:04:08,260 --> 00:04:11,420 perform which allows a component from another app to handle it. 68 00:04:12,440 --> 00:04:16,980 This then goes on to use an example of showing a specific location on a map. 69 00:04:16,980 --> 00:04:20,120 We don't need to create an activity or write special code to show a location. 70 00:04:21,170 --> 00:04:24,980 Most Android devices have some kind of map app on them, like Google Maps. 71 00:04:24,980 --> 00:04:28,620 And if we use an implicit intent to show it, the Android system will present 72 00:04:28,620 --> 00:04:31,920 the user with a list of options that can display a location on a map. 73 00:04:33,220 --> 00:04:36,520 As always, this is best explained with a real example. 74 00:04:36,520 --> 00:04:38,400 We'll come back to that in a little bit though. 75 00:04:38,400 --> 00:04:41,400 Let's stick with explicit intents for a little while longer and 76 00:04:41,400 --> 00:04:44,960 learn next how to pass data back and forth between two activities.