You are currently viewing Android Activity Life Cycle | 5 Easy Step
android activity lifecycle

Android Activity Life Cycle | 5 Easy Step

Android activity life cycle is a process of override of custom method. In android studio a activity is a user interaction layout or screen which consist of some function or elements, to use these control user can move one activity to another activity. 

you can’t become a good android developer while you don’t complete concept the Android Activity Life Cycle.

A activity in android is a single screen or frame. When you design your UI you drop widgets on activity to design attractive UI. There are 7 pre-built in class in java below is graph show how to work these class in android activity.

In c ++ , java and VB.Net start with a main () function similar in android studio initial its program an Activity starting with a method which called OnCreate () method.  

android activity lifecycle

7 Method of Android Activity Life Cycle

Method

Description

onCreate

When a activity is first created

onStart

Call when activity is visible to the user

onResume

When activity is start launching to to user

onPause

When activity not visible to the user

onStop

In this method when a user not perform any action on activity then other activity will resume.

onRestart

Its restart when a activity already stopped.

onDestroy

It done when activity shutdown.

We clear this android activity life cycle with a simple and very basic example “Hello Word” following are the simple step to modify the android studio application. This simple example help you to understand android activity life cycle.

Example :

How To Create your First App

Step 02 : Open your android studio then enter the new package name or application

android activity life cycle

Step 02 : Choose empty activity in your project and then click next.

(Note if you don’t install Android Studio in your PC or laptop so first install this SDK (Software Development Kit) read my previous article how to download and  install Android Studio)

Step 03 :Enter  the name of your app which you want to create its also called package name.

android activity life cycle

 Step 04 :Modify the MainActivty.java with following example.

android activity life cycle
package com.example.myfirst_project;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

String my_msg = "Android : ";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(my_msg, "The OnCreate ()");
}
@Override
protected void onStart() {
super.onStart();
Log.d(my_msg, "The onStart() event");
}
@Override
protected void onPause (){
super.onPause();
Log.d(my_msg,"The OnPause () method");
}
@Override
protected void onStop (){
super.onStop();
Log.d(my_msg,"The onStop () method");
}
@Override
protected void onDestroy (){
super.onDestroy();
Log.d(my_msg,"The onDestroy () method");
}

}

Step 03 : Run your application with you mobile or AVD Manager (If your not installed AVD Manager in your android studio first read this how to install and configure AVD Manager in android studio).

What is Manifest.xml file in Android Studio ?

The Manifest.xml file in android studio have the essential information about your application which your are  developing such as packages, services and components of your activity. It perform some of extra features which are  follow :-

  • Internet Permission
  • User Permission setting for example when your user download your application the app can get the permission which you set in this Manifest.xml file such as contact, location, gallery, internet and much more. It depend on application vendor which information he want to get from their users.
  • Setting up start up activity.
  • All API information.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirst_project">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
android activity life cycle

To learn Android Activity Life Cycle you must have information about the Manifest.xml file. You can find this Manifest.xml file in left side in the project open the app directory and then open the sub directory Manifest. Double click on it and it will be open in right side you can see the code.