Wednesday, July 27, 2011

Activity Life Cycle Example In Android

Activity Life Cycle ::

Below shows the methods of Activity
a)onCreate()
b)onStart()
c)onPause()
d)onResume()
e)onStop()
f) onDestroy()


The above figure shows how and when activity is created and when it is paused and destroyed.
  • <<kill>> means when the resources are low OS will kill the activity.
onCreate() ::This method is called when first time activity is created.
onStart()::This method is called just before your activity becomes visible on the screen.
onResume()::This method is called after onStart() method and if your activity is the foreground activity on the screen.
onPause()::This method is called when your activity is just about to call another activity so that the current activity has to be paused and the new activity has to be resumed. Here the previous activity is not stopped but it loss the foreground visibility means it goes as background activity.
onStop()::This method is called when your activity is no longer visible on the screen.
onDestroy()::This method is called when your current activity has the last chance to do any processing before it is destroyed.

Activity Life Cycle Example ::

Create a new project with the name BasicActivityExample and create a class with the name LifeCycleActivity as a “main” activity.


// LifeCycleActivity.java
package com.android.tutorial;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class LifeCycleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast.makeText(this, "onCreate()", Toast.LENGTH_LONG).show();
}
@Override
protected void onStart() {
//the activity is become visible.
super.onStart();
Toast.makeText(this, "onStart()", Toast.LENGTH_LONG).show();
}
@Override
protected void onPause() {
super.onPause();
Toast.makeText(this, "onPause()", Toast.LENGTH_LONG).show();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Toast.makeText(this, "onResume()", Toast.LENGTH_LONG).show();
}
@Override
protected void onStop() {
//the activity is no longer visible.
super.onStop();
Toast.makeText(this, "onStop()", Toast.LENGTH_LONG).show();
}
@Override
protected void onDestroy() {
//The activity about to be destroyed.
super.onDestroy();
Toast.makeText(this, "onDestroy()", Toast.LENGTH_LONG).show();
}
}

In the above program we wrote all the Life Cycle Methods. Here Toast.makeText(...) method is used to display the text on the screen.

//main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Wait 10 seconds and see the screen shows how methods are executed.\n
After 10 seconds click on ESC on the keyboard see what methods are executed"
/>
</LinearLayout>

The above program is the best and simple example how the activity methods will execute one by one.Remember when the application is started see the bottom it display's which methods are exeuted when an an activity is running.
After all the few seconds click on ESC in the keyboard or press back button in your emulator then you may see the methods which are executed when an application is destroyed.


PLEASE LET ME KNOW IF U ARE FACING ANY PROBLEMS WITH THIS APPLICATION AND U NEED TO KNOW MORE INFORMATION REGARDING THIS ACTIVITY LIFE CYCLE PLEASE COMMENT IT.