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.
and that's what an intent is, it's something that activate a component in your application.
there are 2 types of intent. explicit intents and implicit intents.
with explicit intents, we're giving a specific component that we want to activate, so we wanna activate another activity in our application,
we use an explicit intent to explicitly say, hey i wanna activate that activity.
implicit intents, we'll get into the next movie, but that's essentially a way for you to give a generalized massage,
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,
but for right now let's focus on explicit intents.
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,
and click import and from general here we wanna choose "Existing Projects into Workspace" we'll click next. 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,
inside of that we're gonna go to Ch2. and then we're gonna choose this Explicit_intents folder
so just select that folder. and click ok. and now i'm gonna click finish.
so now it's imported that project into here Eclipse. just quickly take a look at it if we go to the source folder,
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.
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,
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".
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,
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.
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.
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.
ok so what i'm gonna do first is go to my main.java file, and under where we set the contentView,
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.
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".
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,
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.
i'm just gonna call it "b". findViewById , and we're gonna reference to that button. and we'll add the cast.
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.
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.
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.
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,
and i'll call it "intent".
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
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.
so again for the context and this is gonna tell the activity that we're launching who is actually requesting this,
we're just gonna pass in Main.this which is a reference to this main activity.
and for the class we're gonna give the class file for the activity that we want to launch which is Second.class.
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,
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.
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
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.
i'm gonna choose this one and for the key, i'm just gonna call it "thetext".
and now we need to retrieve the data that's inside of that EditText control.
so to do that we simply say et.getText() which is a method that returns text.
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.
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.
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.
because we're referencing it here inside of this anonymous inner event handler. so now we've actually put that data into the intent
and now when we start the activity, that data will actually be sent to the new activity.
so again to start an activity, we're gonna call the start activity method, and we're gonna pass in our intent.
so that's all i have to do here in the main.java file.
again we're responding to the clickListener, creating an intent, where we're going to actually activate the second activity,
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.
in order to retrieve that text value and actually display it in the textView.
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.
let's go back to the java file and let's get a reference to that
so we're gonna create a new textView instance, i'll call it "tv".
and then findViewById, there's our textView.
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.
so to do that we're gonna use the setText method of the textView class.
now in order to get the actual data that was sent with the intent, what we first have to do is to call getIntent.
and this actually will return the intent to us, that was used to activate this particular activity.
and remember, in the main.java we used the putExtra, here we wanna say getExtra and actually let me…
so we're gonna say getExtras which is gonna give us essentially all of the extras that we've put into this intent.
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.
so our's is a string, so i'm actually gonna call getString. and now we pass in the key that we gave when we put the data into the intent, which was "thetext".
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.
so we're gonna launch this, it's an android application.
and let's check out in the emulator.
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".
and we could see that text that i typed in is now presented in the textView in the second activity.
so again this is showing you how to use explicit intents, and by explicit meaning we're actually telling it,
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.
AndroidCode.ir