“Life Cycle of an Activity” which starts right from creating an activity to understanding what happens when we return to any activity and learning how activity gets destroyed, etc. As Activity is one of the basic building blocks of Android, we have to know how it works.
“LIFE CYCLE” What do you understand by this word?
I would say that lifecycle is nothing but a series of changes in the life of anything. It can be a lifecycle of a butterfly or lifecycle of human being whatever it is, and every lifecycle will revolve in a circular manner. For example, let’s have a look on a lifecycle of a frog:
The frog was just to help you relate the android activity lifecycle to real life instances. So before proceeding further, let’s have a look a simple scenario: The way in which you use your phone.
Suppose you are listening to music on your device and at the same time you are checking your mail, and suddenly a message notification came then you switch to messages to read it. After reading it you just go back to your mail. Now a Facebook notification pops up whereby you will switch to the Facebook to the app to see it. Supposing you now want to upload a selfie on Facebook. You open the camera app and take a pic and upload it and then you again go back to your emails.
In this whole scenario do you ever imagine how many lifecycle methods(call-backs) of an activity you are invoking?
We will tell you.
You are creating, starting and resuming first activity along with it some background task are also performing, when an SMS notification came and you switch to SMS app then First activity is paused and second SMS activity is created, started and resumed and then you are returning to the previous app then your current activity goes into the paused state and mail app comes under the resumed state. This will be the same process when you switch between two activities.
Now we know, lots of question are coming to your mind and before you bombard all of them to us we will like to answer them.
The Answer, how it is happening, what was the meaning of the methods (call-backs) that we are talking about is given below.
The Activity class offers some call-backs (also called as methods) to allow the user to switch between different apps and also allow the activity to know the state: whether it is created, paused or stopped, etc.
Below is the flowchart of all the call-backs of the activity and when they will call.
Fig: Life Cycle of an Activity
Now you may have one question in your mind that what is the advantage of this lifecycle?
The advantage of an Activity Lifecycle: –
It helps you to perform specific work which is appropriate for a change of state.
Doing the right things at the right time and switching between app properly will make your app robust and give better performance.
Implementing proper call-backs in your app will avoid crashing if you receive a phone call or switches from one app to other.
It will also save system resources when the user is not currently using it.
It will save your progress when you switch to another app and return after some time. or you move to landscape to portrait mode.
Now we will discuss this blog with the help of a real example.
Suppose we have two Activities in which we have implemented all the call-back methods to show proper flow between the call-backs. And in first activity, there is a button and if we click on that button we navigate to the next activity.
Example With Code:-
Below is the snippet code of onCreate of first activity.
As in this, you can see that we have taken one method “Log.e” and we are passing two string sentences in it. Just don’t worry with this it is very similar to the print method in java. As this method will print the 2nd string as value and 1st as a tag in the android monitor Logcat.
Not able to understand? Don’t worry we have just started. after completing this, all the concepts of the lifecycle will get cleared
can you tell what will happen when I run this application?
Let’s run the application and see what happens in the Logcat .
Now carefully notice on the logcat as you can see there are three statements and if we just focus on the red marked area we found that the string that we have passed in the Log.e are here.
So back to back three methods have been called and onCreate, onStart, and onResume because as we run the app it is created, started and it is resuming as we are working on that activity.
Now let’s have a look at the UI of this first activity.
The arrow in the pic is showing the button.
Now we click on the button to see that now which method has to be called because according to us if we go to next activity then first will get paused and the second will created, started and resume till we work on that.
Let’s have a look on the logcat.
Did you notice that there is one more message, ‘First activity has stopped’? Why did that happen? For the answer just go to the “Activity Lifecycle Chart” in that just look at the flow ……. We are not going to tell just go up and find the answer.
Now you got to know the answer as the first activity is no longer visible that’s why onStop call back is called.
Now you tell me if I press back button from the second activity and go back to first one then what will happen?
Yes! you are learning very quickly. Now the second activity will have paused and the first one will be restarted. Let’ have a look on the Second activity UI and the corresponding logcat.
Now if we press back button then let’s see what will happen in the logcat file.
Below is the logcat file and there you can see that:
Second activity is paused then
First activity is restarted then
First activity is started
First activity is resume
Second activity stops and then it will get destroyed.
But why was second activity is destroyed?
If you go through the Activity Lifecycle, then you may found that an activity will get destroyed by the System. But why System is destroying second Activity not first?
To understand that, you need to understand the relationship between First activity and second activity? Any guesses?
Closely observe that, the second activity is completely dependent on first activity as you cannot open second activity without opening first activity so, when you are in second activity then the system cannot destroy the first because secondly is dependent on the first, but when you return to the first now second activity is no longer in use so the system destroyed it .
Keep Reading Keep Learning! Stay tuned to AcadGild for more interesting Blogs!