Activity Lifecycle – Learn about Activity Lifecycle in Android

2
20

In this video tutorial, you will learn about Activity Lifecycle in Android. Activity lifecycle describes the various stages through which an android activity passes while execution. In activity lifecycle, when an activity is launched, it executes onCreate() function. The code in this function will be executed right after launching android app activity. After it, the control goes to onStart() function and it loads & executes the code in this section. After it the control goes to onResume() function where it resumes if there is anything paused. At this stage, the current status of Activity will be running. If any other activity is launched, onPause() function is invoked. On ending of new activity, control goes to onResume() again where it resumes previous activity. From onPause(), the control can go to onStop(), where to start previous activity, onRestart() function will be invoked. After onStop(), in normal flow, the activity will be destroyed by invoking onDestroy() method.

An example of this can be seen below.

package com.example.imarslan;

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

public class MainActivity extends Activity {
   String msg = "Android : ";
   
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Log.d(msg, "The onCreate() event");
   }

   /** Called when the activity is about to become visible. */
   @Override
   protected void onStart() {
      super.onStart();
      Log.d(msg, "The onStart() event");
   }

   /** Called when the activity has become visible. */
   @Override
   protected void onResume() {
      super.onResume();
      Log.d(msg, "The onResume() event");
   }

   /** Called when another activity is taking focus. */
   @Override
   protected void onPause() {
      super.onPause();
      Log.d(msg, "The onPause() event");
   }

   /** Called when the activity is no longer visible. */
   @Override
   protected void onStop() {
      super.onStop();
      Log.d(msg, "The onStop() event");
   }

   /** Called just before the activity is destroyed. */
   @Override
   public void onDestroy() {
      super.onDestroy();
      Log.d(msg, "The onDestroy() event");
   }
}

 

Watch Next Tutorial: Create IDENTITY, a small project in Android

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here