AdSense

AdSense3

Saturday, 8 August 2015

Android Activity Lifecycle

android activity
Android Activity Lifecycle is controlled by 7 methods of android.app.Activity class. The android Activity is the subclass of ContextThemeWrapper class.
An activity is the single screen in android. It is like window or frame of Java.
By the help of activity, you can place all your UI components or widgets in a single screen.
The 7 lifecycle method of Activity describes how activity will behave at different states.

Android Activity Lifecycle methods

Let's see the 7 lifecycle methods of android activity.
MethodDescription
onCreatecalled when activity is first created.
onStartcalled when activity is becoming visible to the user.
onResumecalled when activity will start interacting with the user.
onPausecalled when activity is not visible to the user.
onStopcalled when activity is no longer visible to the user.
onRestartcalled after your activity is stopped, prior to start.
onDestroycalled before the activity is destroyed.
android activity lifecycle

Android Activity Lifecycle Example

It provides the details about the invocation of life cycle methods of activity. In this example, we are displaying the content on the logcat.
File: MainActivity.java
  1. package com.example.activitylifecycle;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.util.Log;  
  5. import android.view.Menu;  
  6. public class MainActivity extends Activity {  
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.activity_main);  
  11.         Log.d("lifecycle","onCreate invoked");  
  12.     }  
  13.     @Override  
  14.     protected void onStart() {  
  15.         super.onStart();  
  16.          Log.d("lifecycle","onStart invoked");  
  17.     }  
  18.     @Override  
  19.     protected void onResume() {  
  20.         super.onResume();  
  21.          Log.d("lifecycle","onResume invoked");  
  22.     }  
  23.     @Override  
  24.     protected void onPause() {  
  25.         super.onPause();  
  26.          Log.d("lifecycle","onPause invoked");  
  27.     }  
  28.     @Override  
  29.     protected void onStop() {  
  30.         super.onStop();  
  31.          Log.d("lifecycle","onStop invoked");  
  32.     }  
  33.        @Override  
  34.     protected void onRestart() {  
  35.         super.onRestart();  
  36.          Log.d("lifecycle","onRestart invoked");  
  37.     }     
  38.     @Override  
  39.     protected void onDestroy() {  
  40.         super.onDestroy();  
  41.          Log.d("lifecycle","onDestroy invoked");  
  42.     }  
  43. }  

Output:

You will not see any output on the emulator or device. You need to open logcat.
android activity life cycle example output 1
Now see on the logcat: onCreate, onStart and onResume methods are invoked.
android activity life cycle example output 2
Now click on the HOME Button. You will see onPause method is invoked.
android activity life cycle example output 3
After a while, you will see onStop method is invoked.
android activity life cycle example output 4
Now see on the emulator. It is on the home. Now click on the center button to launch the app again.
android activity life cycle example output 5
Now click on the lifecycleactivity icon.
android activity life cycle example output 6
Now see on the logcat: onRestart, onStart and onResume methods are invoked.
android activity life cycle example output 7
If you see the emulator, application is started again.
android activity life cycle example output 8
Now click on the back button. Now you will see onPause methods is invoked.
android activity life cycle example output 9
After a while, you will see onStop and onDestroy methods are invoked.
android activity life cycle example output 10

The onCreate() and onDestroy() methods are called only once throughout the activity lifecycle.

1 comment:

  1. your blog explains life cycle in most easy and beginners learner way

    ReplyDelete