1 00:00:00,000 --> 00:00:09,000 so in the movie where we went over , how do we use activities, you remember that we actually used an intent to activate an activity. 2 00:00:09,000 --> 00:00:15,500 and that's what an intent is, it's something that activate a component in your application. 3 00:00:15,500 --> 00:00:21,000 there are 2 types of intent. explicit intents and implicit intents. 4 00:00:21,000 --> 00:00:31,000 with explicit intents, we're giving a specific component that we want to activate, so we wanna activate another activity in our application, 5 00:00:31,000 --> 00:00:37,000 we use an explicit intent to explicitly say, hey i wanna activate that activity. 6 00:00:37,500 --> 00:00:45,000 implicit intents, we'll get into the next movie, but that's essentially a way for you to give a generalized massage, 7 00:00:45,000 --> 00:00:56,000 hey i wanna do this action and any application on the android system that's capable of that will be able to be used and the user will be able to choose which one they want to use, 8 00:00:56,000 --> 00:00:59,800 but for right now let's focus on explicit intents. 9 00:01:00,000 --> 00:01:06,000 so what we're gonna do is to import a project from the exercise file. so we wanna right click here in the package explorer, 10 00:01:06,000 --> 00:01:15,000 and click import and from general here we wanna choose "Existing Projects into Workspace" we'll click next. 11 00:01:015,000 --> 00:01:24,000 now we gonna browse to the root directory for our project, so i'll click browse and you gonna go to where ever your exercise file are, 12 00:01:24,000 --> 00:01:30,000 inside of that we're gonna go to Ch2. and then we're gonna choose this Explicit_intents folder 18 00:01:30,000 --> 00:01:36,000 so just select that folder. and click ok. and now i'm gonna click finish. 19 00:01:36,000 --> 00:01:43,000 so now it's imported that project into here Eclipse. just quickly take a look at it if we go to the source folder, 20 00:01:43,000 --> 00:01:54,000 into our main package, we have 2 java files that represent the 2 activities in our project. first one's called Main, the second one's called Second. 21 00:01:54,000 --> 00:02:04,000 and there's 2 layout xml files that are used to populate each of those activities. now in the main one we have an edit text view, 22 00:02:04,000 --> 00:02:13,000 which is a control that allows the user to actually to edit an enter text. and then we have a button that says "Go to second activity". 23 00:02:13,000 --> 00:02:21,000 and on the second activity we simply have a text view, so what we're gonna do here is, we're gonna allow user to enter a message, 24 00:02:21,000 --> 00:02:30,000 then when they click go to second activity, it's gonna launch the second activity, and display the text that was written inside of here. 25 00:02:30,000 --> 00:02:38,000 so we're gonna look at how to explicitly launch activities as well as how to actually bundle some data with it to send to the new activity. 26 00:02:38,000 --> 00:02:46,700 and we go to the manifest file. you can see that it's already been declared here, both of the activities in the manifest file. 27 00:02:46,700 --> 00:02:54,000 ok so what i'm gonna do first is go to my main.java file, and under where we set the contentView, 28 00:02:54,000 --> 00:03:03,000 what i'm gonna do is to get references to both that edit text view, and the button that's in our xml so we can work with it. 29 00:03:03,000 --> 00:03:11,500 so let's do the edit text first, so i'm gonna do that by hitting ctrl+space to import the EditText class and i'll just call it "et". 30 00:03:11,500 --> 00:03:24,000 and again like i showed you before to get a reference to it, we're ganna use findViewById, we're gonna do R.id.editText1, 31 00:03:24,000 --> 00:03:32,000 again i'm gonna do a quick fix, to again add that cast to edit text. so now let's do the same thing for button. 32 00:03:32,000 --> 00:03:44,000 i'm just gonna call it "b". findViewById , and we're gonna reference to that button. and we'll add the cast. 33 00:03:44,000 --> 00:03:56,000 ok so now we've done that, we wanna set of the onClickListener for the button so that when user clicks it, it's gonna take them to the second activity. so b.setOnClickListener. 34 00:03:58,000 --> 00:04:08,000 and i'm gonna create a new OnClickListener we're gonna do it as anonymous inner event handler here. hit ctrl+1 to import OnClickListener class. 35 00:04:08,600 --> 00:04:16,000 and now inside the onClick method we can actually respond to that event, so what we're gonna do here is that we want to create a new intent. 36 00:04:16,000 --> 00:04:24,500 and again an intent is basically the way in which we activate components, so i'm gonna create a new instance of the intent class here, 37 00:04:29,000 --> 00:04:31,000 and i'll call it "intent". 38 00:04:34,000 --> 00:04:45,000 it's equal to new intent, and like i mentioned when we talk about activities, when we're actually activating new activities, it's best to use this context 39 00:04:45,000 --> 00:04:53,000 and then pass in the class of the activity we want to launch. now there are other options but this is the one that i find the easiest and that works the best. 40 00:04:53,000 --> 00:05:01,000 so again for the context and this is gonna tell the activity that we're launching who is actually requesting this, 41 00:05:01,000 --> 00:05:06,000 we're just gonna pass in Main.this which is a reference to this main activity. 42 00:05:06,000 --> 00:05:16,000 and for the class we're gonna give the class file for the activity that we want to launch which is Second.class. 43 00:05:16,000 --> 00:05:26,000 so now we could have pass this right here directly into the start activity call, what we're gonna do is that we want it to actually add some data to this intent, 44 00:05:26,000 --> 00:05:37,000 what we wanna add is what ever text that is inside of that editText control, so the way we do that is to use the putExtra method of the intent. 45 00:05:39,000 --> 00:05:46,000 we could see here that there's a bunch of different overrides based on the type of data that we want to send so it's essentially a key value pair 46 00:05:46,000 --> 00:05:57,500 what we give the string for the key and then we can give it a whole host of different data types. so i'm gonna come down here, what we're actually gonna send is a string. 47 00:05:57,500 --> 00:06:05,000 i'm gonna choose this one and for the key, i'm just gonna call it "thetext". 48 00:06:05,000 --> 00:06:10,800 and now we need to retrieve the data that's inside of that EditText control. 49 00:06:11,000 --> 00:06:21,000 so to do that we simply say et.getText() which is a method that returns text. 50 00:06:21,000 --> 00:06:28,000 now one of the things you'll notice is that it doesn't return a raw string value, it returns an instance of the editable class. 51 00:06:28,000 --> 00:06:36,000 but in order to get the raw string value, all i have to do is to say, toString(). and that turns it into just a raw string value. 52 00:06:38,000 --> 00:06:45,000 now one the things it's giving us error about that we can do quick fix here is that we have to change the modifier of "et" to final up here. 53 00:06:45,000 --> 00:06:54,000 because we're referencing it here inside of this anonymous inner event handler. so now we've actually put that data into the intent 54 00:06:54,000 --> 00:06:59,000 and now when we start the activity, that data will actually be sent to the new activity. 55 00:06:59,000 --> 00:07:06,000 so again to start an activity, we're gonna call the start activity method, and we're gonna pass in our intent. 56 00:07:06,000 --> 00:07:10,000 so that's all i have to do here in the main.java file. 57 00:07:10,000 --> 00:07:19,000 again we're responding to the clickListener, creating an intent, where we're going to actually activate the second activity, 58 00:07:19,000 --> 00:07:21,000 but before we do that we're actually putting some data into the intent to send to the new activity, so now we just need to set up second.java file. 59 00:07:21,000 --> 00:07:33,000 in order to retrieve that text value and actually display it in the textView. 60 00:07:34,000 --> 00:07:48,000 so remember inside of the xml file we have a textView component here, and let me actually show you and the Id of this textView which is here is been auto generated. 61 00:07:48,000 --> 00:07:52,000 let's go back to the java file and let's get a reference to that 62 00:07:54,000 --> 00:07:58,000 so we're gonna create a new textView instance, i'll call it "tv". 63 00:08:00,000 --> 00:08:06,000 and then findViewById, there's our textView. 64 00:08:06,000 --> 00:08:17,000 then we do the quick fix to add the cast, so now we have a reference to it, now what we just need to set the actual text inside of it to the data that was sent form the main activity. 65 00:08:17,000 --> 00:08:24,000 so to do that we're gonna use the setText method of the textView class. 66 00:08:24,000 --> 00:08:32,000 now in order to get the actual data that was sent with the intent, what we first have to do is to call getIntent. 67 00:08:32,000 --> 00:08:38,000 and this actually will return the intent to us, that was used to activate this particular activity. 68 00:08:38,000 --> 00:08:47,000 and remember, in the main.java we used the putExtra, here we wanna say getExtra and actually let me… 69 00:08:49,000 --> 00:08:56,000 so we're gonna say getExtras which is gonna give us essentially all of the extras that we've put into this intent. 70 00:08:56,000 --> 00:09:04,000 now we only have one, but again now we have a whole host of methods in which we can retrieve values out of the intent. 71 00:09:04,000 --> 00:09:09,000 so our's is a string, so i'm actually gonna call getString. 72 00:09:010,000 --> 00:09:16,000 and now we pass in the key that we gave when we put the data into the intent, which was "thetext". 73 00:09:19,000 --> 00:09:26,000 ok so again now we're getting a reference to the textView, we're getting the actual data out of the intent and assigning it to that textView. 74 00:09:27,000 --> 00:09:30,000 so we're gonna launch this, it's an android application. 75 00:09:33,000 --> 00:09:35,000 and let's check out in the emulator. 76 00:09:39,000 --> 00:09:50,000 so here it's launched up. and now i have my edit text. so i can type in "this is some text" and i'll click "Go to second activity". 77 00:09:50,000 --> 00:09:57,000 and we could see that text that i typed in is now presented in the textView in the second activity. 78 00:09:57,000 --> 00:10:05,000 so again this is showing you how to use explicit intents, and by explicit meaning we're actually telling it, 79 00:10:05,000 --> 00:10:14,000 exactly the component that we want to launch, next we're gonna look at implicit intents which is a different way to activate components on the android operating system. 80 00:10:14,000 --> 00:10:15,000 AndroidCode.ir