Saturday, 31 August 2013

Acitvity LifeCycle of Android Application


This tutorial explains how android activity lifecycle works. Activity is a window that contains the user interface of your application.


To create an activity your class need to extends Activty base class.An Application has more than on activity . Activity base class contains events that governs the life cycle of an activity.

  • onCreate () : Called when the activity is first created.
  • onStart(): Called when the activity become visible to the user.
  • onResume(): Called when the activity start interacting with the user.
  • onPause(): Called when the current activity is being paused and the previous activity is being resumed.
  • onStop(): Called when the activity is no longer visible to the user.
  • onDestroy(): Called before the activity is destroyed by the system.
  • onRestart(): Called when the activity has been stoped and is restarting again.



Step 1:
Create a project with the following details:
ProjectName      :            LifeCycle
PackageName    :            sat.tuts4mobile.lifecycle
ActivityName        :                LifeCycleActivity  

Step 2:
In the LifeCycleActivity.java file , add the following code:

package sat.tuts4mobile.lifecycle;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
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(LifeCycleActivity.this,"ON CREATE", Toast.LENGTH_SHORT).show();
       
    }
   
    @Override
    protected void onStart() {
       // TODO Auto-generated method stub
       super.onStart();
      
        Toast.makeText(LifeCycleActivity.this,"ON START", Toast.LENGTH_SHORT).show();
    }
   
    @Override
    protected void onResume() {
       // TODO Auto-generated method stub
       super.onResume();
   
        Toast.makeText(LifeCycleActivity.this,"ON RESUME", Toast.LENGTH_SHORT).show();

    }
   
    @Override
    protected void onPause() {
       // TODO Auto-generated method stub
       super.onPause();
   
        Toast.makeText(LifeCycleActivity.this,"ON PAUSE", Toast.LENGTH_SHORT).show();

    }
   
    @Override
    protected void onRestart() {
       // TODO Auto-generated method stub
       super.onRestart();
      
        Toast.makeText(LifeCycleActivity.this,"ON RESTART", Toast.LENGTH_SHORT).show();

    }
   
    @Override
    protected void onStop() {
       // TODO Auto-generated method stub
       super.onStop();
      
        Toast.makeText(LifeCycleActivity.this,"ON STOP", Toast.LENGTH_SHORT).show();

    }
   
    @Override
    protected void onDestroy() {
       // TODO Auto-generated method stub
       super.onDestroy();
      
        Toast.makeText(LifeCycleActivity.this,"ON DESTROY", Toast.LENGTH_SHORT).show();

    }
   
}


Step 3:
When you run the following code , you see when the application starts there are three toast messages comes after one another.
First is ON CREATE , second is ON START and third is ON RESUME.

When you press the home button from the emulator , ON PAUSE and ON STOP methods call.

And when you opens the application again ON RESTART , ON START and ON RESUME methods call.

And in last when you press the back button from emulator , ON PAUSE , ON STOP and ON DESTROY method calls

Click here to download source code of application.

1 comment:

  1. Thanks for the information.

    SyonIndia is a Software Development, Website development and SEO company in Bhubaneswar. We offer professional software development services in Bhubaneswar India. Our developers are highly skilled, advance and expert to execute things up to the mark and are efficient enough to cope up with new platforms. We are the leading software development company in Bhubaneswar.
    http://syonindia.com

    ReplyDelete