Register Login

Activity Lifecycle Diagram in Android

Updated May 18, 2018

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.

package com.example.programmingknowledge.thefirstapp;

import ...

public class MainActivity extends ActionBarActivity {

       @Override
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate (savedInstanceState);
          setContentView (R.layout.activity_main);
       }

       @Override
       public boolen onCreateOptionsMenu (Menu menu) {
             //inflate the menu;  this adds items to the action

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.

public class MainActivity extends ActionBarActivity {

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 the action bar activity. And you can see this action bar activity is inheriting from this class, which is fragment action.

/.../

package android.support.v7.app;

public class ActionBarActivity extends android.support.v4.app.FragmentAction
        private android.support.v7.app.ActionBarActivityDelegate mDelegate;

public ActionBarActivity () /* compiled code */ }

public android.support.v7.app.ActionBar getSupportActionBar () { /*

public void setSupportActionBar (@android.support.annotation.Nullable

public android.view.MenuInflator getMenuInflator () /* compiled code

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.

public class FragmentActivity extends Activity {
     private static final String TAG = "FragmentActivity";

     static final String FRAGMENTS_TAG = "android:support:fragment";

     // This is the SDK API Version of Honeycomb (3.0) .
     private static final int HONEYCOMB = 11;

     static final int MSG_REALLY_STOPPED = 1;
     static final int MSG_REALLY_PENDING = 2;

So now every activity has its 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.

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate (savedInstanceState);
    setContentView (R.layout.activity_main);

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)


×