Intent & Lifecycles
The course is part of this learning path
This course focuses on lifecycles and intent in Android and then moves on to look at services, receivers, and Android app binding.
This course is intended for anyone who wants to learn how to start building their own apps on Android.
To get the most out of this course, you should have some basic knowledge of the fundamentals of Android.
Well, hello everyone. We're going to talk about Intents in this video. And no, not that kind of intense. Just the fact that an Intent is an object and it provides runtime binding between separate components. In other words, between two activities. And at the same time, it provides data transfer between the two activities. So, the Intent represents for an app the "intent to do something". Now, you might imagine it's used for a wide variety of tasks. It does have a very important place in Android application development. We're going to talk about Intents and how they're used to pass between two activities in this particular video. Also, you're going to learn how to transfer data between two activities while passing from one activity to another activity. It might sound a little complex, but we'll go to Android Studio and see it happen.
So, you can tell I've created a new project in Android Studio. You obviously know how to create a new project, I don't need to tell you that anymore. So, first of all, I want to show you an example of the application that we're going to do in this section here. So, what you're looking at is two EditTexts in the design area. Now, one of them is name EditText; the other is number EditText. Now, do you see there's also a button? So, this is going to allow the user to write their name and their age in these different EditTexts. Now, we could write something here for the example and I'll just write in 'David' as the name and I'm writing in '25' as the age. Now, I'm going to press the 'Send' button.
Okay. So, a new activity opens. Also, the user's name and age will be sent to this activity and will be printed in the TextView component there. Now, I'm sure you've noticed there's also an up navigation here. So, when the user clicks this arrow icon, the first activity is going to open back up. Alright, my friends. This is the application that we're going to do. At the end of the lesson, you will have learn how to create a new activity, how to switch between activities, and how to transfer data, as well as how to create an upward navigation button. Alright, that's a lot to chew on. So, let's get started. Alright, away we go. First of all, I'm going to add 'Plain Text' right to the design area. Now, I can add a 'Number' EditText. And then, finally, I'll add a 'Button'. Yeah, you could have done that on your own.
So now, we're going to arrange these components. And I'll make the width of the first EditText component, 300dp. I'll make the height, 50dp. Now, let's also 'Delete' the text of the EditText and add a 'hint' to it. And then, in the hint section, I'm going to just write, 'Please enter your name'. Alright. So finally, let's determine the ID of this EditText component. I'll write, 'editTextName' in the ID section. And now, let's 'Edit' the second EditText. And I'm going to make the width, 300dp, and make the height 50dp. Then, I'll write, 'Please enter your age' in this hint section.
Okay. So now, let's 'Edit' the ID of the EditText component as 'editTextAge'. Alright. So finally, now we can edit the button component. So, I'll make the width of the button, 150dp. And I'm going to change the text so I'll type, 'SEND' in here. Okay. Now, since our main layout is ConstraintLayout, we'll need to determine the constraint values of these components. So, I just choose all three components by 'right-clicking' and select 'Center', 'Horizontally'. And we centered all the components horizontally just like that.
Now, we can determine the vertical constraint values. I'll set the top constraint value of the first EditText component to 100dp and I'd like the top constraint value, second EditText component, 20. And I'll also set the top constraint value of the button to 50dp. Okay. So, that finishes the design of the first activity. So, let's define these components in the MainActivity.kt file. 'lateinit var', the name of this variable can be 'name : EditText'. 'lateinit var', and the name of this variable can be 'age : EditText'. 'lateinit var', the name of this variable can be 'send : Button'.
Now, we got to match their IDs in the onCreate method. 'name =_findViewById(R.id.editTextName)'. 'age =_findViewById(R.id.editTextAge)', alright? 'send =_findViewById(R.id.button)'. Alright. We have now matched the variables that we created here with the components in the design area. Let's add a 'ClickListener' to the button. So, just write in, 'send.setOnClickListener'. And my friends, we will now write the necessary code to open the other activity. And then, send the data over to the other activity. First, we're going to need to create our second activity. So, do you know how that we should create this new activity? Yeah, you might be scratching your head, huh?
Good thing you're not alone anymore. So, what we're going to do is click the 'app' folder from the project directory. And then, after that, select the 'File', new 'Activity', and 'Empty Activity'. So now, I'm going to give a name to this activity and I'll just call it 'SecondActivity' for now. So, when you create a new activity, the layout file of that activity is automatically created, right? You know this. You can also specify this activity as the Launcher Activity. So, if you remember, we mentioned the Launcher Activity while describing the manifest file. So, when users open your application, the Launcher Activity will be the first activity that they encounter.
So again, here, you can see the Package name and the programming language. So, you just press the 'Okay' button without changing any of them. But now, you see how that works, right? So now, I have two activities, So, let's go ahead and design the SecondActivity. There will be only one TextView in the design area. And I'm going to add a 'TextView' to the SecondActivity. And so, what we're going to do is just print the user's name and age in this TextView component so. And I'll also change the layout_width property to 300dp. So, I'm just going to make the text size 24sp. Also, I'll change the textColor property; I'm just going to make it black And then, lastly I will 'Center' the TextView component. Just like that on the design area. So, the ID of the TextView can be TextView.
Alright. So now, I'll define this TextView in the SecondActivity.kt file. So, 'lateinit var', the name of this variable can just be 'result : TextView'. Follow? So, in the onCreate method, I write, 'result =_findViewById(R.id.textView)'. Okay. So, I can go back to the main activity and I want to show you how to open the second activity using Intent, right? That's why we're here. So, I'm going to open up the second activity in the ClickListener of the send button. Right now, how does Intent work? Well first, I'll just create an object of the Intent class. And to do that, I write, 'var intent =_Intent'.
Now, when I call the Intent class, Android Studio imports the Intent class. I'll also set the name of the Intent object as 'intent', but you could, of course, name it whatever you want. Now, this Intent method takes two parameters. First one is the activity name that will start the Intent. So, I'll write '(this@MainActivity)'. The second one is the activity name that will be open using Intent. So, I'll just write 'SecondActivity::class.java'. Now, this method will open the second activity, but I'll need to start this method.
Okay. So, to start the method, I'll write the 'startActivity' function. And inside parenthesis, I'll write the name of the Intent object. And, of course, its name is intent. So, let's run the application and see. So here, the application opens. So, I'll write in a name in the EditText. I'll write in age because it's asking so nicely and click on the 'button'. See? So look, clicking on the button activates the second activity. In the second activity, I now have a TextView.
Alright. So, that means that the Intent is working but it needs to write the name and the age on the TextView. So, in other words, I'll need to write some more code for it. Now, we know how to open another activity using Intent, right? So, if another activity can be open using Intent, can data transfer also be possible? Well, let's see. If we want to transfer data into the second activity, first, we'll need to take this data from the EditText.
Now, how do I do that? Well, let's see. If I click on the button, it should take the name and the age from the EditText, right? Because of this, I'm going to write my codes in the ClickListener. So, I'm going to create a string container. So, that means I'm going to write here, 'var userName : String =_name.text.toString()'. So, this code is going to take the name from the EditText, convert it into the string, and then assign it to this string userName.
Now also, we should create an integer container for the age, right? So, I'll write, 'var userAge : Int =_age.text.toString.toInt()'. Right? So, this code will grab the age from the EditText, convert it into the string, then convert it to the integer, and then assign it to the userAge variable. Then, after all that, I can just use the userName and userAge variables right here in the second activity. So, to make sure that happens, I'm just going to write, 'intent.putExtra()' and that's our function. So, this method will also take two parameters. First parameter is keyword. So, the keyword matches that of the sender and receiver.
Okay. So, I'll write 'username' here. You can use your keyword, but it has to be the same for both sides as well as being unique. Alright. So, the second parameter is data. My data here is userName. Similarly, I'll send the userAge. I'll write 'intent.putExtra'. The keyword can be userage, which is what I'm using and the data will also be userAge. Now, after I write this code, our data will go to the second activity. But, I'm going to need to get them in the second activity, right? They're going to be there but I'm not going to see them. So, that means we've got to get the data inside the onCreate() function. So first, I will get the username. Just below the line where we define the textView component, I'll write 'intent.getStringExtra'.
Now, why did I write that? What does it actually mean? Well, the type of username variable was string. If I were to send a Boolean from the main activity, I would write 'getBooleanExtra'. If I send an integer, I would write 'getIntExtra'. You follow? I can send every piece of data from one activity to another activity using Intent. Anything. So here, I'm just writing 'getStringExtra'. And inside the parenthesis, I'll write the keyword. And if you remember, my keyword is username because it was for the username. That's why I keep it clear. Okay. So, that way we get the username.
Now, we should also assign it to this string container. So this, at the beginning of the line, I'll write 'var userName : String ='. And then, lastly I'll write the 'toString' function out of the parenthesis. And just like that the first data bunch is complete. So now, I'll get a second group of data and that's the userAge. And I'll do it just like I did it before. First, I'll create an integer container. After the equal sign, I'll write 'intent.getIntExtra'. And I'll write two parameters inside parenthesis. First one is the keyword. I'll write 'userage'.
At this point, I would like to point out again, the keywords you use when sending the data and the keywords you use when receiving the data, they got to be matches. They've got to be identical. They have to be exactly the same, right? Otherwise, you won't be able to get that data. You might be scratching your head or deeper still, the developer on down the line will be scratching their head and they got to go in and okay, it's just a waste of time. Make sure they're exactly the same. Yes, it's easy now when it's userage, username. Alright.
The second parameter is the default parameter. If no data comes in, we've got to set a value that would be displayed by default. We can write zero here. So, after that, I'll need to write this data on the TextView. So, I'll write 'result.text ='. Now, inside double quotes, I'll write '"Your name is $userName and your age is $userAge"'. Now, if we wrote a block of code after the dollar sign instead of a single variable name, we would have to use curly braces. But, since we're typing a single variable name after the dollar sign, there is no need for curly braces. Alright. I think everything's ready. Let's just run the application and see if it is.
So, the application opens. Let me just write in the name here. Also, I'm going to write in the age. Now, if you notice, since we use the number EditText for the userage, only the number keyboard shows up as being usable. Right, cool. So, just click on the 'Send' button, right? So, after the send button is clicked, the second activity gets opened, and the name and the age are written in the TextView. What do you think? That is Intent for you. So, by using Intent, you can open another activity and then transfer data between activities.
Now, there is one thing left to do. So, we've got to create a navigation icon so that when the user clicks on this icon, well, we're going to reopen the previous activity, right? So, let's just do that right now so we can finish out the lesson. We're not going to design, we're not going to write any code for this process. But wait, isn't that what this is about? Well, let me show you. In the manifest file, we will define the main activity as the parent activity of the second activity. And then, Android will automatically create a navigation icon for us. See?
So, let's just open up the manifest file. Now, notice here, there are two activity tags. So, we will add the navigation icon to the second activity. Before, inside the tags of the second activity, I'll write 'parent'. Select the 'parentActivityName' property. And the parentActivity of the SecondActivity will be this MainActivity. And that way, when we click on the navigation icon, we want to open the main activity again, right? So now, let's run the application once more and there, the application opens, and let me just write in 'Ronaldo'. And I'm going to be 35 in here. Click the 'Send' button. So there, the second activity has now been opened. Data has been successfully written at the TextView component. Now also, you see this navigation icon has been created. What happens if I click on the navigation icon? Boom. As you can see, we are back to our main activity. Alright, my friends. That's pretty cool. Anyway. Now, you know how to open another activity using Intent as well as transfer data between activities and then go back again. Very cool. So, we're going to take a short break and I'll see you in the next video.
Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.