Android program to implement the Activity Lifecycle

 

Android program to implement the Activity Lifecycle

In Android, Activity is every screen that you see in an Android App. Without User Interface(UI) there can be no Activity. Every screen on Android applications has a lifecycle. When you open any application, there will be a screen which will welcome you. Behind the scenes, this welcome screen was created before it could deliver its contents to your eyes. Without you even realizing, the screen or activity went through few stages in its lifecycle. Every activity has 7 core callback functions mainly onCreate(), onStart(), onPause(), onRestart(), onResume(), onStop() and onDestroy() . Before I move onto the program I will provide you with the explanation for these terms as then you can easily understand what happens in the code.

Below is the source code demonstrating Activity Lifecycle. Here I will be displaying messages with help of Toast and I have also provided with log messages so you can check it on the Android Monitor. You can leave the XML file as it is or just copy it from below.

Steps to create the application:- 

  • Open Android Studio and create a new Android application and name it as “ActivityLifeCycle” and company domain as codedost so your package will be automatically set.
  • Open an Empty Activity and name it as MainActivity.
  • Copy the content of res/layout/activity_main.xml file.
  • Run the application to launch Android emulator or you can run it on your mobile also(which is way faster).

MainActivity.java

package codedost.activitylifecycle;

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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("Activity_Lifecycle","onCreate invoked");
        Toast.makeText(MainActivity.this,"Created",Toast.LENGTH_SHORT).show();
    }
    @Override
    protected void onStart() {
        super.onStart();
        Log.d("Activity_Lifecycle","onStart invoked");
        Toast.makeText(MainActivity.this,"Start",Toast.LENGTH_SHORT).show();
    }
    @Override
    protected void onResume() {
        super.onResume();
        Log.d("Activity_Lifecycle","onResume invoked");
        Toast.makeText(MainActivity.this,"Resume",Toast.LENGTH_SHORT).show();
    }
    @Override
    protected void onPause() {
        super.onPause();
        Log.d("Activity_Lifecycle","onPause invoked");
        Toast.makeText(MainActivity.this,"Pause",Toast.LENGTH_SHORT).show();
    }
    @Override
    protected void onStop() {
        super.onStop();
        Log.d("Activity_Lifecycle","onStop invoked");
        Toast.makeText(MainActivity.this,"Stop",Toast.LENGTH_SHORT).show();
    }
    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("Activity_Lifecycle","onRestart invoked");
        Toast.makeText(MainActivity.this,"Restart",Toast.LENGTH_SHORT).show();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("Activity_Lifecycle","onDestroy invoked");
        Toast.makeText(MainActivity.this,"Destroy",Toast.LENGTH_SHORT).show();
    }
}

XML File(res/layout/activity_main.xml):

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="codedost.activitylifecycle.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Activity Life Cycle"
        android:textColor="#3cff00"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"/>
</RelativeLayout>

Output

Share Me!