In Mobile App Development
3
25 views

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 in an explanatory manner.

Does it sounds somewhat confusing? Well, worry not! Let me explain it in simple terms…

What Is Activity Lifecycle & How Does It Work?

An activity in Android is an important part of an application’s overall lifecycle, and the way the 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.

Given a chance to take an illustration, while playing games or watching videos in mobile if we get a call or message then have you ever noticed 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 you are playing.

It means an 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 user interacts with and every activity in android has 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; that the system is creating, stopping or resuming an activity, or destroying the process.

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 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 an activity stops, you will lose any state and will need to re-create the current state of the user interface when an activity is restarted

Killed

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

Lifecycle Flow

The above picture shows the complete flow of Activity lifecycle.

Activity class provides 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 stars or runs the Application on his mobile.

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 start method we will see the main screen of the application (Ex. Application opened and user 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, 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 entire process of the activity kills or it goes to Resume stage. In most cases, on pause() method called by the system when user press 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 affect the battery life while your activity is paused (Ex. User gets a call or message so automatically the video stops and it will starts when we click).

    Activity

  • onStop(): This call back is called when the activity is no longer visible to the user, it has entered the stopped state.The system may also call 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 call 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 onStart() method.

    Example Code:

    Here the MainActivity class extends from 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();
    }
    }

    Below I have summarized that what methods are called at what time:

    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

    a)onPause()

    b)onStop()

    3. When you backpedal to your application, which was running beforehand

    a)onStart()

    b)onResume()

    c)onRestart()

    4. At the point when the user presses back button to leave the application or there is no memory for the application

    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 about the knowledge for Android Activity Lifecycle in a satisfactory manner. Keep reading for more such updates…

Recent Posts
  • B Uma Sankar

    Congratulations Ashok. Nice topic and must learn for the beginners in Android..

  • Yatash deep sharma

    Awesome work!! I must appreciate the work you have done to put all things in one jar 😉

  • Gopi Raj

    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……

We Are Here to Help!
Name:
Phone:
Email:
Country:
Message:
Captcha:
 

Start typing and press Enter to search