♪ (music) ♪ Okay. Now we will see
explicit intents in action. So I'm going to create
a new project for that, and name it... <i>Explicit Intent</i>. Again, I'm going to create it
as minimum 15 API, and going to use <i>Empty Activity</i>. And main activity will be<i>
Main Activity</i> only. I'll name it as <i>Main Activity</i> only. So in this particular application, I'm going to create one activity as my main activity and there will be a button, and when I click on that button, it will transfer me to my next activity. And the way I do it is by using my <i>Explicit Intent</i>. So this is the layout
for my main activity. I will just... put a new text here. Call it <i>Explicit Intent</i>. And increase the size to 24. Now, create a button. Next. I need to give an <i>OnClick</i> attribute
to this button so that when somebody
clicks on this button, something should happen. So inside this, I will put <i>OnClick</i>. and I will do something. You can name it anything you want to name. And then, click on the text
and press <i>Alt-Enter</i>. That's the shortcut to generate
your meta in your Java file. Otherwise, you could have always
done it manually too. Now my main activity is ready, and the UI for my main activity is ready. All I need to do is put the code
to transfer to my next activity. But for that, I need to create
my second activity. So how do I do that? For that, I need to <i>Right</i> click
on my development [explict], go to <i>New</i>, and... then go to <i>Activity</i> and click on <i>Empty Activity</i>. And name your activity. Let's say I want to name
my second activity as "Second". And do I want to generate
the layout for that too? Yes, I want to generate
the layout for that. Do I want it to be a launcher activity? No. I already have a launcher activity. Do I want to make it
a backward compatible activity also? Yeah. I do want to make it so that's why you need
to check this checkbox too. And the <i>package name</i>
will be this, and press <i>Finish</i>. Now, as you can see there is a Java file, there is an XML file, and there will be an entry
in the manifest file also. There you go. So... whenever you create a new activity, there has to be three steps
you need to take care of-- first a Java file, then an XML file--
that will be a UI part-- and then there should be
a definition of that activity in your XML file too. That is your Android manifest file. Until there is no declaration. Until there is no registration in your Android manifest file, your activity will not work. Your application will not be able
to see your activity. So now I can give any UI
which I want to my activity. Let's just change the background of this. Click on your UI, click on your relative layout, here, and then go to <i>View all properties.</i> Go to <i>Background.</i> And if you know the color code, then you can directly put a color code-- that is a hexadecimal value. Or, if you don't know a color code, you can always choose
this <i>Browse</i> button, let me go through that again. Just click on this <i>Browse</i> button, select a color, and click on <i>Add new resources</i> and click on this <i>New color value</i>. And from here on, whichever the color
suits your requirement, just select that color, and press <i>Okay</i>. And it will get automatically
get you the hexadecimal value. So that's how my second activity
looks like now, the background. If I want to put a <i>TextView</i> here, I can always put a <i>TextView.</i> And I will call it... <i>Explicit Intent.</i> Let me give the ID as <i>36sp</i>. Or let's just call it "Second Activity". There you go. So, my second activity is ready. That's how my second activity looks like. All I have to do is put the code to transfer to my second activity. So for that intent, <i>i1 = new intent.</i> And I need to initialize my intent. For that, as it is an explicit intent, so first source,
source is this activity itself, and second is destination. Which activity I want to transfer,
that is second. So... <i>second.plus</i>. If there would have been
another activity, or any other activity I want to transfer, I will put the name of that activity. And at the end
I need to put <i>Start activity.</i> I need to trigger the intent. And this will transfer me
to my next activity. Let's just see this in action now. Run this on your emulator. As you can see, my application
is up running now, just click on <i>Next</i>. Then, there you go-- you are able to transfer
to your next activity. So when you click on your <i>Back</i> button, you will be transferred
back to your previous activity. And that activity is only present on the-- whenever you click on your <i>Back</i> button, you will get transferred
to your previous activity, as that was the only activity which is present
in your back stack right now. And if you click again back, your application will close, because there is no further activity or previous activity in your back stack now. So that was how you
create an <i>explicit intent</i> and how with the help of <i>explicit intent</i> you can transfer to the next activity. What if you want to transfer the data too? For that purpose, let me just modify my UI now. I will put an added text here where user can put any data what he wants. And I will give
this added text an ID <i>edit1</i>. And I will also give a hint to this, so the user can identify
what he needs to do. Enter your data. So I had this added text now. So whenever a user opens my application, he will be shown this particular<i>
edittext</i> and a button, so he can enter the data
and when he clicks on <i>Next</i> this data should be transferred
to my second activity. And in the second activity if I take a look at the UI-- this is my UI of second activity-- so I need to transfer the data here but I need a mechanism to show that data. So for that, for showing data,
I need a <i>TextView</i> here. So I will put a <i>TextView</i> view here. And I will give the ID
to this ID here, <i>Result</i>. And as of right now, it doesn't show anything, but I will put-- I'll put the text size of 30sp. Okay. And if you want to change a color, of text color, just change again, click on <i>Browser</i>
and select color from here and add <i>New resource</i>-- that will be new color. I just make it red. Okay, cool. Now. Come back to my main activity. First things first, I need my <i>EditText</i> access. So for that, <i>EditText e1</i>, and <i>e1=(EditText)</i>. <i>findviewbyID R.ID.edit1</i>. Now I'm referring to this same <i>edittext</i> which I have designed here. Next thing is I need to put the data inside the intent
before I actually fire it. So that's why I will put
this statement here, that will be <i>i1.putExtra.</i> <i>PutExtra</i> is the method
by which you can transfer the data between two activities
with the help of intents, and this will save the data in a <i>Bundle</i> class object. A <i>Bundle</i> class object
saves the data dynamically, so as soon as you close your application
your data goes away. Firstly, I need a key here, because <i>Bundle</i> class saves data in the form of key and value pair. So I will put any key, let's say "User". The data here,
I don't want to put my own data, but I want to put whatever
the user enters in my <i>EditText</i>, so I already have
the access <i>EditText</i> e1. So I will put <i>e1.gettext().toString</i>,
I need to change it to string. So now I have kept my data inside my <i>i1</i>-- that is my intent,
and after that I'm firing my intent so my data is already passed
to my second activity. Now, all I need is in my second activity, I need to access that data I have. But for that, before I do that, I need the access of my <i>TextView</i>, because this is where
I will be showing my data. So, <i>txt1</i>-- again, <i>txt = (TextView)</i> and <i>findViewbyID.R.id.result</i> Now I'm referring to the same <i>TextView</i>
which I have in my second activity. That's the <i>TextView</i>. Next, I need to get
the access of that intent. So how do I get the access of intent, who has called this particular activity? For that, we need to do <i>Get Intent</i> is the method. Now it will give you the access
of that particular intent who has called the second activity, <i>.getExtras</i>. Now this whole thing will return you a <i>Bundle</i> object. So I will do<i>
Bundle b1 = get intent()</i>. So now, everything
which you have transferred from your first activity
to the second activity, it's now inside your <i>b1</i>. So all I need is to access,
one by one, whatever the data
you want to access from it. Now as of right now, in my <i>Bundle</i>,
there is only one data. So I can put-- and that too is a string, so <i>b1.getString.</i> If it would have been an integer, it would be <i>get int</i>. If it would have been any other data type, it would be accessed accordingly. So here, inside this, here you will put your key. And if you remember, at that point in time, I put <i>User</i>. So here I will put <i>User</i> only. And last, all I need is, I have my data back in my <i>s1</i>, so I need to set this <i>s1</i> to my <i>TextView</i> so that in my second activity
I can see this data. So <i>txt1.setText (s1)</i>. And that's all I need to do
to transfer the data. If I run this application now, so you can see there is added text now. I will transfer the data--
let's say Lalit... Singh. And press <i>Next</i>. As you can see,
the data has been transferred, and the color also has been changed
as I have changed the color while I was designing it. So, if I go back, again, if I change anything here, the data will get changed here. So that was how I transfer
the data between two activities with the help of <i>explicit intent</i>. Thank you. ♪ (music) ♪