Android Activity Lifecycle – A Brief

Whenever you undertake the process of developing Android Applications, the activity lifecycle definitely serves as the fundamental foundation stone for moving ahead towards this approach. The well-known lifecycle of an Android activity covers many stages in between its starting and finishing line, which further needs to be explained.

Does it sound somewhat confusing? Well, don’t worry! Let me explain it in simple terms…

What Is Activity Lifecycle & How Does It Work?

An activity in Android is an important part of the application’s overall lifecycle. The way activities are launched and put together is a fundamental aspect of the Android application model. Every single activity is implemented as a subclass of the Activity base class.

Every Android application runs on its own processes. Processes are started and stopped to run an application and also can be killed to conserve memory and resources. Activities, in turn, are run inside the main UI thread of the application’s process.

Take this example: while playing games or watching videos on mobile, we get a call or message. What will happen to the playing video once you complete your call?

Yes, you will see that your video is stopped and again it starts from where it was when you got that call.

It means the activity is started (playing a video), and once your process is stopped or interrupted by anything, then the activity will automatically stop. In simple words, activity is a screen that the user interacts with. Every activity in Android has a lifecycle like created, started, resumed, paused, stopped, or destroyed. These distinctive states are known as Activity Lifecycle.

The Activity class provides a number of callbacks that allow the activity to know that a state has changed, like:

  • The system is creating,
  • The system is stopping or resuming an activity,
  • The process is destroyed by the system.

More About Activity States…

During its lifetime, an Android activity will be in one of the following states:

State

Description

Running

Activity is visible and the user interacts with it

Paused

If the activity loses focus but remains visible to the user (because a smaller activity appears on top), the activity is paused

Stopped

In case the activity is fully covered by another running activity, the original activity gets stopped. When the activity stops, you will lose any state, meaning you will need to re-create the current state of the user interface when an activity is restarted

Killed

The activity has been terminated by the system by calling to its finish() method.

Activity States - Roadmap of how each state interacts with each otherLifecycle Flow

The above picture shows the complete flow of the Activity lifecycle.

Activity class provides a set of six callbacks to navigate transitions between stages of the activity lifecycle. Better implementation of the lifecycle callbacks can help to avoid crashing, losing the user progress, and screen orientation problems.

Activity launched

When the user starts or runs the Application on his mobile device.

LifeCycle Callbacks:

This section provides conceptual and implementation information about the callback methods used during the activity lifecycle:

  • onCreate(): One must implement this callback. It is nothing but the basic activity creation startup stage. When the application starts, you will find a splash screen until the first main activity screen appears. You must implement this callback to perform basic application startup logic that will happen only once in the whole lifecycle of the activity (Ex. User Application is opening).
  • onStart(): The system invokes this callback when the activity enters the “started” state. Once an activity comes to the start method, we will see the main screen of the application (Ex. the Application is opened and the user is able to see the home screen of the application).

  • onResume(): When the activity enters into this state, the activity stays until the user interacts with any activity such as selecting or clicking on any options for another activity. When an interruptive event occurs, the activity enters the paused state, and the system invokes the onPause() callback (Ex. User Stops an interaction with the app for a while after opening).

  • onPause(): When the user is moving from one activity to another activity, the current activity gets paused, and either the entire process of the activity is killed or it goes to the Resume stage. In most cases, the on pause() method is called by the system when the user presses the Home button (center button on the device). One can use the onPause() method to release the system resources, such as broadcast receivers, or any resource that affects the battery life while your activity is paused (Ex. User gets a call or message so automatically the video stops and it will start when we click).

    Activity

  • onStop(): This callback is called when the activity is no longer visible to the user. This is so because it has entered the stopped state. The system may also call the onStop() method when the activity has finished running and is about to be terminated.     

  • onDestroy(): It is called before the activity is destroyed. This is the final call that the activity receives. It may also call when a screen orientation change occurs, and then immediately calls onCreate() to recreate the activity. The onDestroy() callback releases all resources that have not yet been released by earlier callbacks.

  • onRestart(): It is called when the activity is being restarted, as when the activity is returning to the foreground. It is always followed by the onStart() method.

    Example Code:

    Here the MainActivity class extends from the Activity base class

    package com.loginworks.example;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    
    
    public class MainActivity extends Activity {
        public Main() {
            super();
        }
    
        @Override
        protected void onCreate( Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Log.d("Life cycle","onCreate");
        }
    
        @Override
        protected void onStart() {
            super.onStart();
            Log.d("Life cycle","onStart");
        }
    
        @Override
        protected void onRestart() {
            super.onRestart();
            Log.d("Life cycle","onRestart");
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            Log.d("Life cycle","onResume");
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            Log.d("Life cycle","onPause");
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            Log.d("Life cycle","onStop");
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
        }
    }

    Summary

    1. When you start the application these methods are called by the system automatically

    a) onCreate()

    b) onStart()

    c) onResume()

    2. At the point when some other application is called, while your application was running, the following methods are called:

    a) onPause()

    b) onStop()

    3. When you backpedal to your application, which was running beforehand, the following methods are called:

    a) onStart()

    b) onResume()

    c) onRestart()

    4. At the point when the user presses the back button to leave the application or there is no memory for the application, the following are called:

    a) onPause()

    b) onStop()

    c) onDestroy()

    The onCreate() and onDestroy() methods are called only once throughout the activity lifecycle.

I hope this blog post quenches your thirst for knowledge for Android Activity Lifecycle in a satisfactory manner. Keep reading for more such updates…

Latest posts by Rahul Huria (see all)

3 thoughts on “Android Activity Lifecycle – A Brief”

  1. It’s really going to astonish the Android begineers,great job Mr.Ashok Kumar.I hope we ll get much more information on Android from you……

    Reply

Leave a Comment

ProofFactor