Paramclasses Academy - Blog

Tutorials Details

←
Post 13 of 40
→

What Is an Android Activity? Activity Lifecycle Explained with Java Examples ?

What Is an Android Activity? Activity Lifecycle Explained with Java Examples ?

November 2, 2026

1. Introduction

Every Android application needs a way to display screens and interact with users.

For example, when you open a login screen, view a profile page, or open a settings screen, Android uses an Activity to manage that screen.

An Activity is one of the fundamental components of an Android application.

Android also manages the Activity lifecycle. This lifecycle describes how an Activity is created, displayed, paused, stopped, and destroyed.

In this tutorial, we will learn:

  • What an Android Activity is.

  • How to create an Activity using Java.

  • What the Activity lifecycle means.

  • Important lifecycle methods.

  • How to understand lifecycle execution using Logcat.

  • How to save and restore simple UI state.

2. What Is an Android Activity?

An Activity is an Android application component that provides a screen where users can interact with the application.

For example:

  • LoginActivity displays a login screen.

  • MainActivity displays the home screen.

  • ProfileActivity displays user information.

  • SettingsActivity displays application settings.

An application can contain multiple Activities. Each Activity can represent a different screen or part of the user experience.

Simple Example:

Imagine a shopping application.

When a user opens the application, the Home Activity displays products. When the user selects a product, another Activity may display its details.

Each Activity manages its own screen and user interactions.

3. How to Create an Activity in Android Studio

Let's create a simple Activity using Java.

Step 1: Create an Android Project

  1. Open Android Studio.

  2. Create a new Android project.

  3. Select a project template that uses Java.

  4. Enter your application name.

  5. Finish project setup.

Android Studio may create a MainActivity.java file automatically, depending on the selected template.

Step 2: Open MainActivity.java

Write the following code:

package com.example.myapp;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

        setContentView(R.layout.activity_main);
    }
}

Understanding the Code

AppCompatActivity

AppCompatActivity is a base class that provides compatibility support for Android Activities.

onCreate()

This method is called when the Activity is created.

super.onCreate(savedInstanceState)

This calls the parent class implementation of onCreate().

setContentView()

This method connects the Activity with its XML layout.

For example:

setContentView(R.layout.activity_main);

It loads activity_main.xml as the screen layout.

4. What Is the Activity Lifecycle?

The Activity lifecycle describes the different states an Activity passes through during its lifetime.

Android manages these states depending on user actions and system conditions.

For example:

  1. The user opens an application.

  2. Android creates its Activity.

  3. The Activity becomes visible.

  4. The user interacts with it.

  5. Another screen partially covers it.

  6. The Activity may become stopped.

  7. The Activity may eventually be destroyed.

Android provides lifecycle callback methods that allow developers to respond to these changes.

The main lifecycle methods are:

  • onCreate()

  • onStart()

  • onResume()

  • onPause()

  • onStop()

  • onRestart()

  • onDestroy()

Let's understand them individually.

5. onCreate() – Activity Creation

onCreate() is called when Android creates an Activity.

It is commonly used to:

  • Initialize variables.

  • Connect XML views.

  • Set up click listeners.

  • Load the initial screen.

  • Prepare required resources.

Example:

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

    setContentView(R.layout.activity_main);

    System.out.println("Activity Created");
}

Simple Understanding:

When an Activity is created for the first time, Android calls onCreate() to initialize it.

6. onStart() – Activity Becomes Visible

onStart() is called when the Activity becomes visible to the user.

At this stage, the Activity is visible, but it may not yet be ready for user interaction.

Example:

@Override
protected void onStart() {
    super.onStart();

    System.out.println("Activity Started");
}

Simple Understanding:

onStart() runs when the Activity is becoming visible on the screen.

7. onResume() – Activity Becomes Interactive

onResume() is called when the Activity enters the foreground and becomes ready for user interaction.

It is a suitable place to:

  • Start animations.

  • Resume camera previews.

  • Resume resources needed while the Activity is active.

  • Register listeners that should only work while the Activity is in the foreground.

Example:

@Override
protected void onResume() {
    super.onResume();

    System.out.println("Activity Resumed");
}

Simple Understanding:

When the Activity is ready for the user to interact with it, onResume() is called.

8. onPause() – Activity Loses Foreground Focus

onPause() is called when the Activity is losing foreground focus.

For example, another Activity or a system interface may appear in front of it.

It is commonly used to:

  • Pause animations.

  • Pause camera previews.

  • Stop short-running foreground interactions.

  • Save small changes that should not be lost.

Example:

@Override
protected void onPause() {
    super.onPause();

    System.out.println("Activity Paused");
}

Important: onPause() does not always mean that the Activity is completely hidden. Another screen may only partially cover it.

Keep onPause() work relatively short because Android expects the Activity to respond quickly.

9. onStop() – Activity Becomes Invisible

onStop() is called when the Activity is no longer visible to the user.

For example, when another Activity completely covers it, the previous Activity may enter the stopped state.

It can be used to:

  • Release resources that are not needed while the screen is invisible.

  • Stop unnecessary background UI work.

  • Save appropriate application state.

Example:

@Override
protected void onStop() {
    super.onStop();

    System.out.println("Activity Stopped");
}

Simple Understanding:

onStop() runs when the Activity is no longer visible.

10. onRestart() – Activity Starts Again

onRestart() is called when a stopped Activity is about to start again.

For example:

  1. The user opens an Activity.

  2. Another Activity appears completely over it.

  3. The first Activity becomes stopped.

  4. The user returns to the first Activity.

In this situation, onRestart() is called before onStart().

Example:

@Override
protected void onRestart() {
    super.onRestart();

    System.out.println("Activity Restarted");
}

Simple Understanding:

onRestart() runs when a stopped Activity is about to become visible again.

11. onDestroy() – Activity Destruction

onDestroy() is called when an Activity is being destroyed.

It may happen when:

  • The Activity is finishing.

  • The system is recreating the Activity due to a configuration change.

Example:

@Override
protected void onDestroy() {
    super.onDestroy();

    System.out.println("Activity Destroyed");
}

Important: Do not depend on onDestroy() for saving important application data. Android may terminate an application process without giving the Activity an opportunity to execute its destruction callback.

Save important data at appropriate points in the lifecycle.

12. Understanding the Activity Lifecycle Flow

When an Activity is opened for the first time, the usual callback sequence is:

onCreate()
    |
onStart()
    |
onResume()
    |
Activity Running

When another Activity completely covers it:

onPause()
    |
onStop()

When the user returns to the stopped Activity:

onRestart()
    |
onStart()
    |
onResume()

When the Activity is finished:

onPause()
    |
onStop()
    |
onDestroy()

These are common sequences, not guaranteed sequences for every situation. For example, an Activity that is only partially covered may receive onPause() without immediately receiving onStop().

13. Practical Example: Implement All Activity Lifecycle Methods

Now let's implement all the main lifecycle callback methods in one Java program.

MainActivity.java

package com.example.myapp;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "ActivityLifecycle";

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

        setContentView(R.layout.activity_main);

        Log.d(TAG, "onCreate called");
    }

    @Override
    protected void onStart() {
        super.onStart();

        Log.d(TAG, "onStart called");
    }

    @Override
    protected void onResume() {
        super.onResume();

        Log.d(TAG, "onResume called");
    }

    @Override
    protected void onPause() {
        super.onPause();

        Log.d(TAG, "onPause called");
    }

    @Override
    protected void onStop() {
        super.onStop();

        Log.d(TAG, "onStop called");
    }

    @Override
    protected void onRestart() {
        super.onRestart();

        Log.d(TAG, "onRestart called");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        Log.d(TAG, "onDestroy called");
    }
}

Understanding the Code

TAG

private static final String TAG = "ActivityLifecycle";

TAG is used to identify our log messages in Logcat.

Log.d()

Log.d(TAG, "onCreate called");

Log.d() prints a debug message in Android Studio Logcat.

We use it to understand when each lifecycle method is executed.

14. How to Check Activity Lifecycle Using Logcat

Now let's run the application and observe the lifecycle callbacks.

Step 1: Run the Application

Run your Android application on an emulator or real Android device.

Step 2: Open Logcat

Open Logcat from Android Studio.

Step 3: Search Using TAG

Search for:

ActivityLifecycle

Step 4: Observe the Initial Execution

When the Activity opens, you should normally see:

onCreate called
onStart called
onResume called

Step 5: Press the Home Button

When the Activity moves to the background, you will commonly see:

onPause called
onStop called

Step 6: Open the Application Again

When the stopped Activity becomes visible again, you should see:

onRestart called
onStart called
onResume called

This practical test helps you understand the Activity lifecycle instead of only memorizing its methods.

15. How to Save and Restore Activity State

Sometimes an Activity is recreated because of a configuration change, such as rotating the device.

If we do not save the required UI state, entered information may be lost.

Android provides onSaveInstanceState() to save small amounts of temporary UI state.

Let's understand with an EditText example.

XML Code – activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp"
    android:orientation="vertical">

    <EditText
        android:id="@+id/etName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter your name" />

</LinearLayout>

Java Code – MainActivity.java

package com.example.myapp;

import android.os.Bundle;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    EditText etName;

    private static final String KEY_NAME = "saved_name";

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

        setContentView(R.layout.activity_main);

        etName = findViewById(R.id.etName);

        if (savedInstanceState != null) {
            String savedName =
                savedInstanceState.getString(KEY_NAME, "");

            etName.setText(savedName);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

        outState.putString(
            KEY_NAME,
            etName.getText().toString()
        );

        super.onSaveInstanceState(outState);
    }
}

Understanding the Code

onSaveInstanceState()

This method saves small pieces of UI state in a Bundle before the Activity may be destroyed.

putString()

It stores a String value inside the Bundle.

getString()

It retrieves the saved String value.

savedInstanceState != null

This checks whether saved state is available.

If it is available, we restore the saved name inside the EditText.

Important: Saved instance state is intended for temporary UI state. For important long-term data, use persistent storage such as SharedPreferences or a database.

16. Activity Lifecycle vs Application Lifecycle

An Activity lifecycle and an application process lifecycle are not the same thing.

Activity Lifecycle:

It describes the state changes of an individual Activity.

Examples include onCreate(), onResume(), and onStop().

Application Process Lifecycle:

It relates to the lifetime of the application's process.

Android manages application processes according to system resources and background requirements.

An Activity can be stopped while its application process remains alive. The system may also terminate a background process when resources are needed.

Therefore, developers should not assume that an application's process will always remain active.

17. Common Activity Lifecycle Mistakes

Mistake 1: Forgetting super methods

When overriding lifecycle callbacks, call the appropriate superclass implementation.

Mistake 2: Saving all data only in onDestroy()

Important data should be saved before it can be lost. Do not depend on onDestroy() being called.

Mistake 3: Performing long operations in onPause()

Keep onPause() work short so the Activity can transition quickly.

Mistake 4: Confusing onPause() and onStop()

onPause() means the Activity is losing foreground focus. onStop() means it is no longer visible.

Mistake 5: Assuming onCreate() runs every time the Activity becomes visible

onCreate() is called when the Activity instance is created. Returning to a stopped Activity normally calls onRestart(), onStart(), and onResume() instead.

Mistake 6: Ignoring configuration changes

Screen rotation and other configuration changes can recreate an Activity. Handle important UI state appropriately.

18. Practice Tasks

Try these tasks yourself:

Task 1: Create a simple MainActivity using Java.

Task 2: Override all seven main lifecycle methods.

Task 3: Print lifecycle messages using Log.d().

Task 4: Run the app and observe Logcat when opening and closing the Activity.

Task 5: Press the Home button and reopen the application. Observe the callback sequence.

Task 6: Create an EditText and save its value using onSaveInstanceState().

Task 7: Rotate the device and check whether the entered text is restored.

19. Conclusion

Activity is one of the most important components of Android application development.

It provides a screen where users can interact with an application.

In this tutorial, we learned:

  • What an Android Activity is.

  • How to create an Activity using Java.

  • What the Activity lifecycle means.

  • How onCreate() initializes an Activity.

  • How onStart() and onResume() make it visible and interactive.

  • How onPause() and onStop() handle foreground and visibility changes.

  • How onRestart() starts a stopped Activity again.

  • How onDestroy() handles Activity destruction.

  • How to observe lifecycle methods using Logcat.

  • How to save and restore temporary UI state.

Understanding the Activity lifecycle helps developers manage resources, preserve UI state, and build reliable Android applications.

20. Frequently Asked Questions (FAQs)

Q1. What is an Activity in Android?

An Activity is an application component that provides a screen where users can interact with an Android application.

Q2. What is the Activity lifecycle?

The Activity lifecycle describes the different states an Activity passes through from creation to destruction.

Q3. Which method is called first when an Activity is created?

onCreate() is the first Activity lifecycle callback when a new Activity instance is created.

Q4. What is the use of onResume()?

onResume() is called when the Activity enters the foreground and becomes ready for user interaction.

Q5. What is the difference between onPause() and onStop()?

onPause() means the Activity is losing foreground focus. onStop() means the Activity is no longer visible.

Q6. When is onRestart() called?

onRestart() is called when a stopped Activity is about to start again.

Q7. Is onDestroy() always called when an application closes?

No. Android may terminate an application's process without calling onDestroy() on its Activities.

Q8. What is onSaveInstanceState() used for?

It saves small amounts of temporary UI state so they can be restored if the Activity is recreated.

Q9. How can we check Activity lifecycle methods?

We can use Log.d() and Android Studio Logcat to observe lifecycle callback execution.

21. SEO Details

SEO Title: What Is an Android Activity? Activity Lifecycle Explained with Java Examples

Meta Description: Learn what an Android Activity is and understand its lifecycle methods including onCreate, onStart, onResume, onPause, onStop, onRestart and onDestroy with Java examples.

Focus Keyword: Android Activity Lifecycle

Related Keywords:

  • What Is Activity in Android

  • Android Activity Lifecycle Methods

  • onCreate in Android

  • onResume and onPause

  • Android Activity Lifecycle Java Example

  • Android Studio Java Tutorial

  • Activity Lifecycle with Logcat

Suggested URL Slug: android-activity-lifecycle-java-examples

←
Post 13 of 40
→