Explicit vs Implicit Intents4:45 with Ben Jakuben
Intents come in two varieties: Explicit and Implicit. We use explicit intents when we explicitly name the class of the target activity that will handle the intent. Implicit intents are used without a class name, where Android will help determine an appropriate Activity to handle the intent.
Let's start with one of the most common scenarios we encounter in Android, 0:00 passing data from one activity to another. 0:04 If you've been taking other courses here, then you've already seen this, of course. 0:06 But let's take a deeper look at what Android is doing for us, so 0:09 we can better understand how intents work and the different ways we can use them. 0:12 A quick note. 0:17 I forgot to include a new app icon in the initial project files. 0:18 If you want to use it, download it from the teacher's notes, delete the default 0:21 icons here in the res > mipmap folder, and then put the new icon file in here. 0:26 All right, so let's close this build.gradle file and 0:31 open up MainActivity. 0:33 So expand Java and then the package and double click on it. 0:35 Now for this brief exercise, let's hijack the code in our download button. 0:41 So I'm gonna scroll down a little bit and 0:45 we have an OnClickListener here in the OnCreate method for our download button. 0:48 I'll start by refactoring this code into a method. 0:53 So I'm going to select everything, right click on it, 0:55 then refactor, Extract into a Method. 1:00 Make sure we select MainActivity, not an anonymous method. 1:04 And let's call this downloadSongs. 1:08 Hit Enter. 1:14 And okay, so now it's down at the bottom. 1:16 Our code was collapsed. 1:19 I'll just click on it to expand it and a space and 1:19 I'm going to comment this code out for now. 1:23 Let's create a new method in here to simply test intents. 1:27 Now if we go back to this method and use Alt+Enter, our quick fix, 1:34 we can create this new method. 1:39 And once again select MainActivity. 1:42 And here we go we have a new method stub. 1:44 In here all we want to do is create a new intent. 1:47 Let's call it intent. 1:50 And then we'll use the constructor new Intent that expresses our intent to 1:53 start a new activity which needs the current contexts as the first parameter. 1:57 And the name of the target activity as the second parameter, DetailActivity.class. 2:02 Now to start this activity, we call startActivity and pass in this new intent. 2:11 So let's try this out just to make sure that it's working. 2:17 Okay, here's app and since we hijacked the download button. 2:24 Let's tap on that. 2:27 All right, and here's our second activity. 2:29 So let's take another look at this code. 2:31 This intent, where we name the class that's going to handle it, 2:35 is known as an explicit intent. 2:39 Let's take a quick look at some of the documentation on the Android Developer's 2:46 site about intents. 2:49 If we scroll down just a little bit, we see a section here on intent types. 2:50 There are two types of intents, explicit intents and implicit intents. 2:55 So here in explicit, note that it says you'll typically use an explicit intent to 3:00 start a component in your own app because you know the class name of the activity or 3:05 service you want to start. 3:09 So right back here, 3:11 we are explicitly using the class name of the target activity. 3:13 So what do you think it means to use an implicit intent? 3:17 What intent would we imply without explicitly using a known class? 3:20 This is where the true power and beauty of intents comes into play. 3:25 This is something I love about Android. 3:29 We can use intents to easily leverage other parts of Android, or 3:31 other apps, to help us do things in the context of our own app. 3:35 Intents change our device from a grab bag of independent apps 3:39 to a system of integrated components. 3:43 What I really mean is this. 3:46 Implicit intents allow us to imply a solution to handling an intent 3:47 without explicitly defining what is going to handle that intent. 3:52 In other words, we say we wanna do something without knowing exactly 3:56 how to do it, but trusting the Android system to help us figure it out. 3:59 Or if we take a look at the definition in the docs, we declare a general action to 4:03 perform which allows a component from another app to handle it. 4:08 This then goes on to use an example of showing a specific location on a map. 4:12 We don't need to create an activity or write special code to show a location. 4:16 Most Android devices have some kind of map app on them, like Google Maps. 4:21 And if we use an implicit intent to show it, the Android system will present 4:24 the user with a list of options that can display a location on a map. 4:28 As always, this is best explained with a real example. 4:33 We'll come back to that in a little bit though. 4:36 Let's stick with explicit intents for a little while longer and 4:38 learn next how to pass data back and forth between two activities. 4:41
You need to sign up for Treehouse in order to download course files.Sign up