Android Activity Lifecycle With Example & Source Code

Activity Lifecycle:- Activity is one of the building blocks of Android OS. In simple words Activity is a screen that user interact with. Every Activity in android has lifecycle like created, started, resumed, paused, stopped or destroyed. These different states are known as Activity Lifecycle. In other words we can say Activity is a class pre-written in Java Programming.
Below is Activity Lifecycle Decription:--
onCreate() – Called when the activity is first createe
onStart() – Called just after it’s creation or by restart method after onStop(). Here Activity start becoming visible to user
onResume() – Called when Activity is visible to user and user can interact with it
onPause() – Called when Activity content is not visible because user resume previous activity
onStop() – Called when activity is not visible to user because some other activity takes place of it
onRestart() – Called when user comes on screen or resume the activity which was stopped
onDestroy – Called when Activity is not in background
Android Activity Lifecycle Diagram
Generally, in android activity class uses different callback methods like onCreate(), onStart(), onPause(), onRestart(), onResume(), onStop() and onDestroy() to go through a different stages of activity life cycle
Following is the pictorial representation of Android Activity Life cycle which shows how Activity will behave in different stages using callback methods.
Android Activity Lifecycle Example:-
we  are going to create example of lifecycle : File Code
package com.example.helloworld;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("Activity Lifecycle","onCreate invoked");
    protected void onStart() {
        Log.d("Activity Lifecycle","onStart invoked");
    protected void onResume() {
        Log.d("Activity Lifecycle","onResume invoked");
    protected void onPause() {
        Log.d("Activity Lifecycle","onPause invoked");
    protected void onStop() {
        Log.d("Activity Lifecycle","onStop invoked");
    protected void onRestart() {
        Log.d("Activity Lifecycle","onRestart invoked");
    protected void onDestroy() {
        Log.d("Activity Lifecycle","onDestroy invoked");

Post a Comment