Lessons: 8Length: 44 minutes

Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.5 Activity Lifecycle

From the time an app is opened to the time it is closed, you will be navigating between Activities. That means Activities will be opening and closing, so you’ll need to manage them so they don’t leak memory or tie up the processor. In this lesson, you will learn about the Activity lifecycle.

2.5 Activity Lifecycle

So in this lesson, we will learn about activity lifecycle. Now, while you change an activity or perform some action when you move from one activity to other, of course, it has a lifetime, and it goes through a number of states. Android uses are series of call back function to handle the transition between the states. And all these callback functions comprises of the activity lifecycles. The callback functions include the onCreate() function, the onStart() function, onResume(), onRestart(), onPause() and onStop() and onDestroy(). I'll show you the sequence in which each of this is executed, and what are the significance of these functions. Let's go see a graphical example of how these functions are executed. Now this is the home screen of my Android device. And this is my app, on clicking which, my first activity, or let's say the main activity will get started. On clicking this, my main activity is on the process of creation, or let's say, the onCreate function is called. In this onCreate function, we declare and initialize all the views and visits that will be used for our application. But keep in mind the activity is not ready yet, after onCreate, the onStart function is called. It is executed soon after onCreate exists. The activity now becomes visible to the user, but it is not ready for the user to be used, or let's say the user cannot interact with this activity. After onStart, onResume is executed. This is invoked just before the activity starts interacting with the user. At this point, the activity is at the top of the activities stack. Now, once the onResume is exited, the user can interact with the activity and the views in it. Most of the app's core functionality is implemented on the onResume method before it becomes available for the user to be used. Now let's say we have a button in our main activity which will take us to the second activity. When the user moves from one activity to other, let's see what happens. On clicking this button, the Explicit Intent will take us to the second activity. For the first activity, the onPause function will be executed. Here, the activity loses the focus and enters the pause state. Now the first activity is on pause whereas the second activity has entered the onCreate function for which all its views and widgets is being initialized, but it is not visible to the user. After onCreate, onStart is executed, the activity is now visible to the user but he can't interact with it, same like the first activity. After onStart, we have onResume and after onResume the second activity becomes visible to the user. And once the second activity is visible to the user, the onStop method is executed for the main activity. The main activity moves out of the scene. It is not visible to the user since now the activity which is visible is the second activity. Let's say you want to go back to the main activity by clicking this back button. On clicking the back button the onPause function for the second activity will be executed where the activity will just lose the focus. When the onPause for the second activity runs, the onRestart for the first activity is executed because we want to restart the main activity which is already created. After onRestart, onStart is called for the main activity. And here, you can see the onCreate method is skipped, the views and widgets are not reinitialized. We are restarting the main activity without carrying out the work load again. After the onStart of the main activity, onResume method is called. And once the main activity is visible to the user, for the second activity, the onStop method is executed. After onStop, onDestroy is executed to get the activity destroyed. Now we no longer have the second activity in the stack. This is to ensure that all of the activity's resources are released when the activity is destroyed. Coming back to the main activity, when you click on the back button for the main activity to exit the application, the onPause will be executed in which the main activity will lose its focus, onStop and then onDestroy. onDestroy ensures that all the resources of the main activity is destroyed and then you will exit the application. So this is what activity lifecycle is all about. This is the sequence in which the lifecycle methods are executed. Let's move to Android Studio, and see ourselves, how this works. In Android Studio, I have created a new project in which I have the main activity. This is the example file for the main activity. I have changed the layout to RelativeLayout and I have a button for it which will take me to the second activity. I have created a second activity as well for which the layout is activity_second. In activity_second, I just have a text view just to indicate that this is the second activity. Now let us write the code to see how the activity lifecycle works. To do that, we will just overwrite the life cycle methods and add the log statement to it, just to see the sequence of the executed methods. So let us create a string here which we will use as tag in our log statements. And yes, this is the function which will be executed when the button of taking the user to the second activity is clicked. In the onCreate function, we will write the log, And just write the function name, hitting Alt + Enter, and it's done. So we have written the log statement for the onCreate function, now let us override onStart. And then adding the log statement. Similarly we will overwrite all other life cycle methods. Similarly, we will do this for the second activity. Yeah, so in the second activity we have a lifecycle methods overwritten. Yes we have onRestart too, I forgot that. Now we are done overwriting the lifecycle methods, now let us open Logcat, Changing it to Info. Let us run our application and see the sequence of methods in which they are executed. See, this is our main activity and here, we have the third main activity coming from here the onCreate method, onStart, OnResume, and then the activity has become visible. Now on moving to the second activity, OnPause, OnPause for the main activity, and then in the second activity, we have Create, Start, Resume. The activity is visible. When the second activity is visible, the main activity execute onStop. Keep in mind it is not destroyed yet, it is just stopped and it's not visible to the user. On hitting back and going to the main activity, The second activity is onPause. For the main activity, onRestart is executed, then we have onStart. Once the main activity is started, it is resumed and the activity is visible to the user. When the first activity is visible to the user, the second activity stops and is then destroyed by calling onDestroy. Now we have the second activity completely destroyed. On hitting back again, for the main activity, we have OnPause and then OnStop. And finally, we have OnDestroy called for the main activity, and it is completely destroyed and is out of context. This screen we are getting from the previous application which was opened. So never mind, again onCreate, start, resume, on hitting back, onPause, stop, destroy. So that is all about the activity lifecycle. So for the onCreate it is called when the activity is first created, and we make use of the bundle parameters to retrieve all the views, widgets and the states of the activity. It is followed by onStart. For the onStart, it is called when the activity is becoming visible to the user. onResume, is always followed by onPause and the activity is completely in the foreground When the onResume is executed. onRestart, it is called when the activity is started again after it was stopped, it is always followed by onStart. onPause is executed when the activity starts to go in the background. For onStop, the activity is completely in the background. And finally the onDestroy is called, and the activity is destroyed. These are the seven activity lifecycle methods which are very important. But we have other two lifecycle methods, they are onRestoreInstanceState and onSaveInstanceState. I'll talk to you about this in the next lesson. These are used in case of the configuration changes or the screen rotation. See you in the next lesson.

Back to the top