Translate

Activity Lifecycle in Android

Introduction:-
Activity is one of the building blocks of Android OS. In simple words, Activity is a screen that a user interacts with. Every Activity in Android has a lifecycle like created, started, resumed, paused, stopped or destroyed. These different states are known as Activity Lifecycle.


Activity Lifecycle:-

onCreate() - Called when the Activity is first created.

onStart() - Called just after it's creation or by restart method after onStop(). Here Activity start becoming visible to the user.

onResume() - Called when Activity is visible to the user and the user can interact with it.

onPause() - Called when Activity content is not visible because user resumes previous activity.

onStop() - Called when Activity is not visible to the user because some other activity takes place of it.

onRestart() - Called when user comes on screen or resumes the activity which was stopped.

onDestroy() - Called when Activity is not in background i.e. it has been closed by the user (by closing the Application).

Activity Lifecycle Diagram
Activity Lifecycle in Android




Sample Application to depict Activity Lifecycle in Android:-

Step 1:- Create a New Project with Application name, say - "ActivityLifecycleDemoApp".


Step 2:- Write the following code in activity_main.xml file,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    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="Activity 1"
        android:textColor="#000000"
        android:textStyle="bold"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="38sp"
        android:id="@+id/txtRandom" />

</RelativeLayout>  


Step 3:- Now write the following code in MainActivity.java file,

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        showLog("Activity Created");

    }

    @Override

    protected void onRestart(){

        super.onRestart();//call to restart after onStop

        showLog("Activity restarted");

    }

    @Override

    protected void onStart() {

        super.onStart();//soon be visible

        showLog("Activity started");

    }

    @Override

    protected void onResume() {

        super.onResume();//visible

        showLog("Activity resumed");

    }

    @Override

    protected void onPause() {

        super.onPause();//invisible

        showLog("Activity paused");

    }

    @Override

    protected void onStop() {

        super.onStop();

        showLog("Activity stopped");

    }

    @Override

    protected void onDestroy() {

        super.onDestroy();

        showLog("Activity is being destroyed");

    }

    private void showLog(String text){

       Log.d("HOME_ACTIVITY_TAG", text);

    }


}
  
Step 4:- Now execute your android application in Android Emulator or in mobile phone using ADB. Search out for the tag - "HOME_ACTIVITY_TAG" in Logcat window and see the different lifecycle methods being called as the android application execution makes progress.

Comments

Popular posts from this blog

Step-by-Step tutorial to build an android application which reads text from an image like OCR, using Google Vision API

Tutorial to make a SimSimi Chatbot Android Application

Step-by-Step Tutorial to make an Unlock Splash Screen Android Application

Wanna use Themed WhatsApp.....??

Lenovo's 'all-screen' Z5 phone has a notch too...!!

5 Sites and Extensions to Tweak YouTube for a Better Experience

Web Terminology

5 Sites and Apps to Find Movies & TV Shows to Watch on Netflix

How to Stop LinkedIn From Telling Someone You Viewed Their Profile

Lockhunter lets you delete the files that Windows restricts from deleting