Android Activity Lifecycle

In this tutorial you will learn about android activity lifecycle.

I have already told you in previous tutorial that an activity is a screen or window that interacts with user. It holds all the UI widgets like button, label, textbox, etc.

If you have worked on Java language then you would definitely know that Java program execution starts with main() method. Similarly in Android, Activity class also have a call back method named as onCreate(). When an activity is created, the first method called is onCreate().

There are 6 other call back methods that are called at different stages in activity lifecycle. The java class associated with an activity should extend Activity class. And that java class must override at least onCreate() method.

Below I have shared a flow chart that shows different stages of android activity lifecycle.

Android Activity LifecycleImage Source

onCreate() – It is called when the activity is first created.

onStart() – It is called when the activity becomes visible to the user.

onResume() – It is called when the user starts interacting with the application.

onPause() – This method is called when the current activity is being paused and the previous activity is being resumed.

onStop() – It is called when the activity is no longer visible to the user.

onDestroy() – It is called before the activity is destroyed by the system.

onRestart() – It is called when the activity restarts after stopping it.

 

Android Activity Lifecycle Example

Below I have shared an example to demonstrate android activity lifecycle. In this example I have used Log.d() method to show log messages. It is a method used for debugging purpose.

Just create a new project with package name com.thecrazyprogrammer.helloworld and paste following code in MainActivity.java file and run the app. Try to observe the log messages in logcat window.

package com.thecrazyprogrammer.helloworld;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("Log-------","onCreate()");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("Log-------","onStart()");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("Log-------","onRestart()");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("Log-------","onResume()");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("Log-------","onStop()");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("Log-------","onDestroy()");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("Log-------","onPause()");
    }
}

 

You can open log messages by following the steps marked in below image. Click on different buttons like menu, call, back, etc on your emulator and observe the different log messages to understand when and which call back method is called.

Android Activity Lifecycle Example

If you found anything incorrect or have doubts related to above android activity lifecycle tutorial then feel free to comment below.

1 thought on “Android Activity Lifecycle”

Leave a Comment

Your email address will not be published. Required fields are marked *