Activity Lifecycle
Start course
2h 15m

This course focuses on lifecycles and intent in Android and then moves on to look at services, receivers, and Android app binding.

Intended Audience

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.


Course GitHub repo: 



Guess what? It's the next video. So, hello. We're going to talk about this Activity Lifecycle. Now, I'm sure you've heard me mention it before, I know you've probably heard about it before. It is important, and I'll tell you why. So, we're going to focus on the activity instead of the application. Now, how does the lifecycle work in the activity? So, there is a lifecycle algorithm and it's also valid for the activity. We already talked about how the lifecycle works in one of the previous videos, so I don't want to necessarily continue that here, but I do want to show you in practice in Android Studio. So, we're going to cruise on over to there. Now, you've got to remember that we've already got a project cooking in Android Studio and I just want to continue with this project. Now, we've added log records to see the methods in our project. So, I'm going to add some components to this project to see how and show you how the activity cycle works. So, what I'll do first is I will add to this main activity, one TextView and two buttons.

All right, so now I've got three components. I'm going to organize them so that we can see them all together. Also, I'm going to change the layout, height and width of this textView to 50dp, and for layout height, 50dp is obviously too big, so I'm going to change it to 30. Now, we will change the text to the textView to zero, and also, I'm going to change the textSize of the textView to 24dp, alright. So, text color of the textView can be, let's just call it white and the background color let's just make it green. And the gravity of the textView can be center. Okay, so I've done all of this to see the number on a textView. Well, comfortably, right, clearly we can see everything. Now, when I click this button, I want to add to this number plus one. I will change the text of the button to +1. Also, I'll have another button here when I click this button I want to go to another activity. So, I will need that other activity.

Remember how to create another activity. So, click on the 'app folder'. After that, select the 'file', 'new activity' and 'empty activity'. Now, I need to give it a name and I'm just going to call it SecondActivity. Click 'Finish'. So, Android Studio will create a SecondActivity for me. Alright, so this is now the SecondActivity and this is the main activity. Now, when I click on this button, I want to open the second activity. So, I'll change the text of this button and I'm going to write just 'Go to the second activity', Now, after that, I just write the necessary code for these components. So, first I'll define the components, I will write lateinit var_textView : TextView and button1 : Button and button2 : Button. Now, continue in the onCreate method, textView = findViewById(, button1= findViewById(, button2 = findViewById( Great, so after that, just add a ClickListener to button1, button1.setOnClickListener. Now, also I'm going to add a counter here, alright. So, var counter = 0. Now, every time that I click the button one I want the counter to increase by one. So, I write counter = counter + 1. And also, I really should write the new value of this counter to the textView here. textView.text = " " + counter. Now, if I add " " before the counter, what's going to change the integer counter to the string, and this is just a quick way. So, now it will increase the counter every time I click this button and it will write it to the textView. So, after that I'm going to add another ClickListener to button2, button2.setOnClickListener. Now, when I click this button it should open the second activity for me. Right, so how do I do that? I will use an intent for this, so var intent = Intent(this@MainActivity, Now, when I click on button2, it will open the second activity for me. Now, when I go between the two activities, I want to see which methods work. So because of this, I will write the first activity expression before the onCreate word. So, this will show me the onCreate method works for the First Activity. So, I will copy this First Activity word, and I will paste it to all of the methods. Alright, so I'm done with the first activity. So, when I click this +1 button, it will increase the number in the TextView. And then, also when I click on this button, it will open the second activity. So, now I can just pass on over to the second activity. So, I will add a Button to the second activity. This button is for going back to the main activity, alright. I will take this button into the center. I will change the text of the button and I will write 'Go to activity one'.

So, now when I click on this button, it should open the first activity, and I'll need to define this button in the SecondActivity.kt file. I'll just write in here, lateinit var button: Button. In the onCreate method, the button =findViewById( Now, add a ClickListener to this button, button.setOnClickListener. So, after that I will be able to use the same intent codes for this button, var intent = Intent(this@SecondActivity, cool. And finally, startActivity(intent). Now, I'm also going to need log records for this activity to see which method works. So copy the log records from the main activity and paste them into the second activity. I'm also going to change the first activity expression, and instead of that, I will write second activity, and I can do the same for the rest. Okay, so after all that, the second activity is now ready. So when I click on this button, it will, should anyway open the main activity and when I click on this button, it will open the second activity. And also if I click on this button, it will increase number +1. Now, before running the application, I will set the screen orientation property to the full sensor from the 'manifest file', because I want to show you which lifecycle functions work when we change the orientation of the phone. Right? So, inside the activity tag, I will make the full sensor the screen orientation property, all right. So, let's open up the log records. Now, make sure that the 'Debug' record is selected here. And also that 'message' is written in the search bar here, right. I delete the previous log records by clicking the 'trash' icon here. Yes. Now, let's run the application. Application opens and now, as you see, when the application opens, the three methods worked. First Activity onCreate, First Activity onStart, First Activity onResume. So, if I click the 'GO TO SECOND ACTIVITY' button, what will happen? Wow, when I click the 'GO TO SECOND ACTIVITY' button, the first activity on pause method worked. After that, the second activity onCreate, onStart, and onResume methods work. After the second activity opened, the first activity onStop method work. So now, here's where you've got to be careful. I will click the 'GO TO ACTIVITY ONE' button and when, I click the 'GO TO ACTIVITY ONE', the second activity onPause method works. And after that the first activity onCreate, onStart, and onResume worked, cool. And that means the first activity is created once again. So, after the first activity opened. Second activity onStop method worked, okay. So you might be thinking hey pal, we've already gone through this. I get it. Why are you going on about it? Alright, so why is this important?

I will show you right here. Now you remember I put a button in here. When I click this button it will increase the counter +1 and write it on textView. Now, I'm going to click this button six times and now it writes six on the textView. Then, after that I'll go to the second activity. It opens to second activity after that I go back to the main activity. And when, I go back to the main activity you see here 'the counter'. What's it say? So, when I go to the second activity, the main activity gets closed and when it's closed everything disappears. So, when I go back to the main activity, the main activity is created once again, and because of this starts from zero, deletes everything that I've done. So, if I want to keep the counter value I must save it somewhere before the activity is closed, right? So, that means I want to teach you how to save your data. Now, it also happens, by the way, when I rotate the phone. So, let me click this button again. Now the count is five. After this, I'll rotate the phone in the landscape position. So, when I turned the phone the first activity onPause, onStop, and onDestroy methods work. And then, after that onCreate, onStart, and onResume methods work for the first activity. So, that means that when I just turned the phone the activity is destroyed, and then created again. So, when I turned the phone the counter gets reset. So what does that mean, then? Well, before you turn the phone, you need to save the values too. Now we're going to learn all of that in some of the later videos. Well, I just wanted to bring home to you how that activity lifecycle works. You get it now. Alright, so that's enough for now. I'll see you in the next video.

About the Author

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.

Covered Topics