Activity Lifecycle in Android with an Example App

Android Activity Lifecycle: In this tutorial, we will learn about the activity lifecycle in Android with the help of a demo (example) application. By Shamikh Faraz Last updated : June 06, 2023

What is an Activity Lifecycle in Android?

Activity Lifecycle contains 7 methods of the 'Activity' class. These 7 methods will define how an activity functions in different states. The activity is the subclass of ContextThemeWrapper class. Activity is also used to place UI components/widgets on a single window. In general, an activity is a screen/window in Android.

The Methods of Android Activity Lifecycle

The following are the seven methods of an activity lifecycle in Android:

  1. onCreate(): This is called when an activity creates.
  2. onStart(): This is called when an activity becomes visible to the user.
  3. onResume(): This is called when the user starts interaction with the app.
  4. onPause(): This is called when an activity does not receive any input from the user and cannot execute any code and is called when the previous activity is being resumed as well.
  5. onStop(): This is called when the activity is no longer visible to the user.
  6. onRestart(): This is called when the activity restarts after stopping it.
  7. onDestroy(): This is called before the activity is destroyed by the system/user.

Methods Explanation

When you start your app the following methods are called:

onCreate()
onStart()
onResume()

When your app goes to the background by opening another app the following methods are called:

onPause()
onStop()

When you go back to your app the following methods will be called:

onRestart()
onStart()
onResume()

When you permanently close your app the methods are called as below:

onPause()
onStop()
onDestroy()

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

Diagram of Activity Lifecycle in Android

Diagram of Activity Lifecycle in Android

Example: Activity Lifecycle Demo App

XML File: (activity_main)

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.faraz.activitylifecycle_example.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

JAVA File: (MainActivity.java)

package com.example.faraz.activitylifecycle_example;

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);
        setContentView(R.layout.activity_main);
        Log.d("lifecycle","onCreate called");
    }
    @Override
    protected void onStart() {
        super.onStart();
        Log.d("lifecycle","onStart called");
    }
    @Override
    protected void onResume() {
        super.onResume();
        Log.d("lifecycle","onResume called ");
    }
    @Override
    protected void onPause() {
        super.onPause();
        Log.d("lifecycle","onPause called ");
    }
    @Override
    protected void onStop() {
        super.onStop();
        Log.d("lifecycle","onStop called ");
    }
    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("lifecycle","onRestart called ");
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("lifecycle","onDestroy called ");
    }
}

Output

Emulator or your device can not provide output. For output, you have to go to 'logcat'. Click Run icon from the toolbar, it will only display emulator, but output in LogCat window:



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.