Activity in Android


14th December, 2022

In this tutorial we will learn one of the most important topics of Android app development, which is Android Activity.

What is Activity?

Activity is referred to as a screen of an Android Application. Activity is one of the building blocks of Android OS. An Android app consists of one or more screens or activities. We can say that Activity is a class pre-written in Kotlin or Java programing.


This is an example of a chat screen (activity) of a chatting application. e.g. WhatsApp

This screen will open when the user taps on a chat from the message list or starts a new chat.

You can type a message, view chats, record/send audio messages, and do more using the chat screen

User can also navigate from this screen to another screen(Activity).

In other words, Android Activity provide a frame for users to interact with and perform their tasks.

Android Activity Lifecycle

Every Android Activity goes through a lifecycle or different stages. The various stages of each activity in an Android application are managed by the Android Activity Stack.

There are four major stages in an Activity.

⇒ Running stage
When an activity is shown on the user’s phone screen, it is in the running state.
⇒ Paused state
An Activity is not in focus but still alive for the user. It is in paused state.
⇒ Resumed state
When the Activity goes from paused state to the foreground. This is the resumed state.
⇒ Stopped state
Activity is in the stopped state when it is not in the activity stack and is not visible to the users.

Android Activity Lifecycle Methods

Activity has lifecycle methods like created, started, resumed, paused, stopped or destroyed. Please see the image below that describes the lifecycle of Android Activity.

Activity Lifecycle in Android
Method Description
onCreate() The onCreate() method is invoked when the activity is first created. It is executed once during the lifetime of the Activity. After onCreate() method, the onStart() method is executed.
onStart() Activity is not yet rendered on screen but is about to become visible to the user during the execution of the onStart() method. In this method, you can perform operation related to UI.
onRestart() When activity is about to start from the stop state. the onRestart() method is called. This method is to restart an activity that had been active some time back.
onResume() When Activity is rendered on the screen, the onResume() method is invoked. Now user can interact with the Activity. Activity is in the Active state.
onPause() When Activity losses its focus, the onPause() method is invoked. At this time Activity is going into the background but has not yet been killed. Activity is partially visible to the user. You can commit database transactions and perform light-weight processing.
onStop() The onStop() method is invoked when activity is getting destroyed or another existing activity comes back to resume state. Activity is no longer visible to the user.
onDestroy() When an activity is destroyed by a user or Android system, the onDestroy() method is invoked. Activity is not in background.

Take a look at the code of the Android activity lifecycle methods in MainActivity.

package com.example.myapplication1

import android.os.Bundle

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        //Called when the activity is first created

    override fun onStart() {
        //Called after it's creation or by restart method after onStop().
        //Activity is not yet rendered on screen
        //but is about to become visible to the user.
        //You can perform operation related to UI

    override fun onResume() {
        //Called when Activity is visible to user
        //Activity is in the active state
        //user can interact with Activity

    override fun onPause() {
        //Called when Activity losses its focus
        //Activity is going into the background but has not yet been killed
        //You can commit database transactions and perform light-weight processing

    override fun onStop() {
        //Called when activity is getting destroyed or
        //another existing activity comes back to resume state
        //Activity is no longer visible to the user

    override fun onRestart() {
        //Called when user comes on screen or resume the activity which was stopped

    override fun onDestroy() {
        //Called when an activity is destroyed by a user or Android system
        //Activity is not in background

Declare an Activity In Androidmanifest.xml

To implement in the application we have to register each and every Android activity in Androidmanifest.xml file. The <activity> tag is used to declare an activity.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    xmlns:tools="" >

        tools:targetApi="31" >

        <!--Declare an Activity-->
            android:exported="true" >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                android:value="" />


Happy coding!

Leave a Reply