Android activity life cycle
Android activity life cycle

Hi Guys, Welcome to Proto Coders Point, In the Android Article we will discuss on the LifeCycle of android Activity.

Android App Lifecycle – Android Activity Lifecycle

Android App Lifecycle is been build by 7 methods which are in android.app.Activity class library.

What is Activity in android? An activity is a page or a screen in android application where application user create different Event. An activity is same like window or frame of java.

The 7 android lifecycle method of Activity class, will instruct the app how to behave at particular states.

Android activity lifecycle methods

Then, let’s learn the 7 lifecycle methods in android activity.

MethodDescription
onCreate ()called when app Activity is first started.
onStart()called when activity is been ready to show to users.
onResume()called when activity start interacting with users.
onPause()called when user switch to other app, or when user minimize the app
onStop()called when activity is not visible to user.
onRestart()After activity is stoped and user reopens the app.
onDestroy()called when user close the app.

Android activity lifecycle diagram

android activity lifecycle flowchart
android activity lifecycle flowchart

Activity lifecycle program in android studio

Create a new Android Project in android studio with Empty Activity.

then just copy paste the respective source codes

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".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" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.example.androidlifecycle;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("lifecycle","onCreate invoked");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("lifecycle","onStart invoked");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("lifecycle","onResume invoked");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("lifecycle","onPause invoked");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("lifecycle","onRestart invoked");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("lifecycle","onStop invoked");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("lifecycle","onDestroy invoked");
    }
}

Logcat output

android lifecycle logcat output
android lifecycle logcat output

Conclusion

In the Android Article we learnt what is an android activity and the LifeCycle of Activity.