Android Activity Lifecycle

As a user navigates through the app, Activity instances in your app transition through different stages in their life-cycle. The Activity class provides a number of callbacks that allow the activity to know that a state has changed: that the system is creating, stopping, or resuming an activity, or destroying the process in which the activity resides.
Now let's know the Android Activity Life Cycle in a more detailed manner with the help of life cycle methods and callbacks.


Life Cycle Methods and Callbacks

In general, activity lifecycle has seven callback methods:

Method Name Description
onCreate() called when activity is first created.
onStart() called when activity is becoming visible to the user.
onResume() called when activity will start interacting with the user.
onPause() called when activity is not visible to the user.
onStop() called when activity is no longer visible to the user.
onRestart() called after your activity is stopped, prior to start.
onDestroy() called before the activity is destroyed.

Now let's get into the details of Android Activity Life cycle methods and callbacks. Take a look at the below figure to understand the life cycle.

So these are the various methods of the Activity Life Cycle. Now let's see the situations where the life cycle methods and states will occur.

MainActivity.java

package com.theitbulls.activitylifecycledemo;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;

public class MainActivity extends AppCompatActivity {
    private AppCompatButton btnLifeCycleDemo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main_activity);

        btnLifeCycleDemo = findViewById(R.id.btnLifeCycleDemo);
        btnLifeCycleDemo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("ActivityLifeCycle: ", "State: onStart()");
        Toast.makeText(this, "State: onStart()", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("ActivityLifeCycle: ", "State: onResume()");
        Toast.makeText(this, "State: onResume()", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("ActivityLifeCycle: ", "State: onStop()");
        Toast.makeText(this, "State: onStop()", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("ActivityLifeCycle: ", "State: onPause()");
        Toast.makeText(this, "State: onPause()", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("ActivityLifeCycle: ", "State: onRestart()");
        Toast.makeText(this, "State: onRestart()", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("ActivityLifeCycle: ", "State: onDestroy()");
        Toast.makeText(this, "State: onDestroy()", Toast.LENGTH_SHORT).show();
    }
}

Logcat Result

Download: ActivityLifeCycleDemo.zip