Activity Lifecycle Android

There are seven methods of android.app.Activity class that controls the Android Activity Lifecycle. The ContextThemeWrapper class has the Android Activity as a subclass. A single screen in android, much like a window or frame of Java is what an activity is. Thus, all the UI components or widgets can be placed on a single screen, using the Activity.

Android Activity Lifecycle methods:

The lifecycle methods of Activity that describe the behavior at different states are described below:

Method Uses
onCreate To be called when the activity is first created.
onStart To be called when the activity is becoming visible to the user.
onResume To be called when the activity will start interacting with the user.
onPause To be called when the activity is not visible to the user.
onStop To be called when the activity is no longer visible to the user.
onRestart To be called when the activity is stopped, prior to start.
onDestroy To be called when the activity is destroyed.

Android Activity Lifecycle Example:

In the below example, we are providing the details about the invocation of life cycle methods of activity, by displaying the content on the logcat.

File: activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="MainActivity">
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Good Morning!!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
 
</android.support.constraint.ConstraintLayout>

File: MainActivity.java:

package com.example.radioapp;
 
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("lifecycle","onCreate invoked");
    }
    @Override
    protected void onStart() {
        super.onStart();
        Log.d("lifecycle","onStart invoked");
    }
    @Override
    protected void onResume() {
        super.onResume();
        Log.d("lifecycle","onResume invoked");
    }
    @Override
    protected void onPause() {
        super.onPause();
        Log.d("lifecycle","onPause invoked");
    }
    @Override
    protected void onStop() {
        super.onStop();
        Log.d("lifecycle","onStop invoked");
    }
    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("lifecycle","onRestart invoked");
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("lifecycle","onDestroy invoked");
    }
}

Output:

  • No output will be displayed on the emulator or device, for which the logcat needs to be opened.
  • The onCreate, onStart and onResume methods will be invoked on the logcat.
  • On clicking the HOME Button, we can see that the onPause method is invoked.
  • The onStop method will be invoked after a while.
  • The emulator will get back to the home. To launch the app again click on the center button.

  • Click on the lifecycle activity icon.
  • The onRestart, onStart and onResume methods will be invoked on the logcat.
  • The application will start again on the emulator.
  • On Clicking the back button, we can see that the onPause method is invoked.
  • The onStop and onDestroy methods will be invoked after a while.

It is clear that throughout the activity lifecycle, the onCreate() and onDestroy() methods are called only once.