Android Activity Lifecycle at first can look daunting and rather tiresome. If you want to advance your programming. You need to understand what is going on inside the application.
So I have written this post for you to make things a little bit clearer. I do hope this post will help you understand the life-cycle of android activity.
What is Android Activity Lifecycle ?
When you create an activity you see the
onCreate() method on the activity.
Have you ever asked where is the
onCreate() activity called ? Because we don’t call these methods anywhere else even for one time.
And more importantly why do we write everything inside the
onCreate() and not the constructor ?
To understand what activity life-cycle is we need to first understand why we write code inside onCreate().
We write everything in
onCreate() because the Android system will call the method internally when we start the activity.
It’s our job to start an activity and it’s android system’s job to invoke the
Now, then when will the
onCreate() method be called by the android system ? The answer to this question is when the system wants to call it. But it’s not called randomly, it is predefined in the system.
Did you understand the explanation ? If you have then you just understood the what Activity Lifecycle is. It’s just a sequence in which the system calls it’s method.
And there are other similar methods, take a look at image below to understand what I mean
The image above clearly shows the sequence of method calls. And that is exactly how the Android Activity Lifecycle works.
Let us look at each method below.
This method is called by system internally. It is called when the system let’s the application know the activity is ready to show UI components. It is done through the callback pattern using interface.
setContentView() is called there, which sets the UI layout. And normally you do all the findViewById() on this method.
And there is one thing you must know it has a related callback method.
onDestroy(), more about it below.
This is the method related to
onCreate() method above. But it will be only called when the activity is being destroyed.
Actually it is called before the activity is completely destroyed. As after destroying the activity it cannot be invoked.
And to destroy an activity means to either call
finish() or killed by system.
This method is called immediately after the
onCreate() method. And when this method is called, the activity is visible to the user.
onCreate() method above is called before the activity is visible, i.e. when it is instantiated. But this method gets called by the system when the activity is visible.
It has another related method i.e.
This method is called when the activity is no longer visible on the screen. One example is, if the user opens up another application from recent.
Also it is called right after onPause() if the application is exiting.
But here is something more interesting about being in the onStop() phase. Since, the activity is no longer visible, the system can kill the process, if more memory is required.
If the system kills the process when the user navigates back to activity. The
onCreate() method is called again because it is a new process as old one was terminated.
But if the process was not killed by system. It will invoke the
onRestart() method and then
So you can also use the
onRestart() method to put necessary logic for re-visibility of you activity.
Now, this method is called when something interrupts the activity. And by interrupting I mean something comes above the screen. Such as a dialog box, notification popup, etc.
Also, this method is also called just before onStop() if the activity is being exited.
The related method for onPause() is onResume(). It is explained below.E
This method is called when the activity regains the control from short interruption. That is right after when the dialog dismisses or the notification bar goes away.
This method is also called right after
Why follow Lifecycle anyway ?
Now that you have learned about activity lifecycle in Android. This question might just pop on your mind. Why should you care about activity lifecycle ?
To keep things short I will just answer it in simple way. This questions are only related to beginner. So you can feel free to skip it if you already know the whole process.
You are writing codes for a mobile platform. And unlike desktop they have their ways of managing the memory and power. For this reason they designed these devices to follow some patterns. And these patterns make them smooth and power efficient.
Hence, the lifecycle is adhering to the guidelines of the system design. They are the point when certain things are triggered by system. And it’s the developer’s responsibility to perform appropriate task at that stage.
You will learn these things once you dive deeper into the development.
I hope I made sense in this post in an easy way. You can read more related contents below.