What is Android Activity Lifecycle
Whenever you go inside this Java folder in your app and click the main activity, the main brain or main logic of your Android Activity Application is the Java class which inherits from activity class.
But when you take a look at the extend which means inheriting from this class, but you don’t see that it’s inheriting from the activity class, it’s inheriting from action bar activity.So it’s like multiple inheritances and at last, the base class or super class here is the activity class.
So to demonstrate this, this main activity is inheriting indirectly from activity class what you can do is, you can just press control plus or control and hover over this action bar activity and click it. And you will reach to the action bar activity. And you can see this action bar activity is inheriting from this class, which is fragment action.
So you can just press control and hover over this fragment action and click it and now you can see this fragment action extend from the activity. So indirectly your main activity is inheriting from activity class.
So now every activity has it’s life cycle. For example, when your program or activity launches, the first method it looks for or it calls is the method shown below which is the onCreate method and we are just overriding this method from the class we are inheriting from. So here we are calling it and the first thing when we start our application, is we want to launch the layout or set the view of the layout.
In the same way, here you can call a different method. For example, whenever you run your Android app, the user can resume this and he can go to some other Android app.
And our actual application was running in the background because it was not closed but it’s running in the background. But you can close this app also by going back and close it. So there are some methods, which are called when you do all these actions. When you go to your menu here, you can call your app by clicking on the app and it will open like the picture attached below.
So when you click the app icon or when you do some other cool things with your application, what are the methods which are called this you can write in the Android activity lifecycle. And the overview of these methods you can see on Android website in the documentation.
Android Activity Lifecycle Diagram
You will see a diagram below which will demonstrate the basic lifecycle of an activity and what happens when you launch your application.
The first method which is called as is onCreate method. And this onCreate method calls the setContentView method, which sets the layout for you for the activity.
Then you add onStart method or override this method onStart this method is called when your activity starts. So after creating your layout you can call this method. So this is the chronological lifecycle of your activity. So as shown in an image the methods which are called one by one if you add these methods in your Java file main activity Java file.
Then you can resume the application or pause the application and in between this application runs. So the chronological order of these functions, which can be called by your activity. So resume then activity is running then if you want to call another activity, for example you are on the homepage of your application and you want to see the about page of your application, you go or you click some button and then the activity which is running is paused and then your new activity is going to start. And then if you unpause that, the activity is no longer visible and stopped.
Whenever you shut down or close your application, it destroys your activity and shuts down. So you can go to this page and you can read about these activities and these functions – what every function means.
Read Next About Android Application for Adding Two Numbers (Simple Calculator)