Thursday, March 14, 2013

Activity In Android

 Activity in Android :

An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View).

An Activity represents   an UI with which a user can Interact.

Each activity in an application goes through its own lifecycle. Once and only once when
an activity is created, is the onCreate() function executed. If the activity exits, the
onDestroy() function is executed. In between, various events can lead to the activity
being in multiple different states, as illustrated in Figure .



As seen here, various common actions by the user can cause the activity to be paused,
killed, or even launch multiple versions of the application.


I have created an application which shows all the states of an Activity  and also attached the logs in which we can clearly see and understand about the life cycle of an Activity.


onCreate() :     Called when the activity is first created. This is first method called when an Activity  starts. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one. generally in this method we write setContentView()  method and inflate the XML layout.

onStart(): Called when the activity is becoming visible to the user.

onResume():  Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it.

onPause():  Called when when you start a New Activity , the previous Activity goes in Pause state and pushed in stack.  When we come back on previous  Activity , Previous Activty gets Poped from Stack and  onResume() method of previous Activity gets called.

onStop(): Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one. This may happen either because a new activity is being started, an existing one is being brought in front of this one, or this one is being destroyed.

onDestroy() : called when the  your activity is going to be destroyed.

The following Code/Example Illustrate all these things . You can copy the code and  run. I have displayed Toasts showing the states of Activity .

Here I have 2 Activities  1: DemoFirstActivity   2: Second Activity

Inside DemoFirstActivity  i have started Second Activity

We have two XML Layouts here  
main.xml  for  DemoFirstActivity
layout2.xml  for Second Activity


main.xml  :


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
>

  
     <TextView
         android:id="@+id/textView1"
         android:layout_gravity="center_horizontal"
         android:textSize="23dp"
         android:layout_marginTop="150dp"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="This Is Fist Activity Activity"
         />
  
    <Button
        android:id="@+id/button1"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="      Second Activity     "
        android:onClick="startSecondActivity"/>

</LinearLayout>




layout2.xml :


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
>

     <TextView
         android:id="@+id/textView1"
         android:layout_marginTop="150dp"
         android:layout_gravity="center_horizontal"
         android:textSize="23dp"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="This Is Second Activity" />

      </LinearLayout>

   

DemoFirstActivity.java



public class DemoFirstActivity 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, "On Create Called In First Activity", Toast.LENGTH_LONG).show();
            Log.i("FirstActivity", "Inside onCreate");

           
    }
     
   
    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        Toast.makeText(this, "On Start Called In First Activity", Toast.LENGTH_LONG).show();
        Log.i("FirstActivity", "Inside onStart")
;
    }
   
   
    @Override
    protected void onResume()
    {
        // TODO Auto-generated method stub
        super.onResume();
       
        Toast.makeText(this, "On Resume Called In First Activity", Toast.LENGTH_LONG).show();
        Log.i("FirstActivity", "Inside onResume");

    }
   
   
   
 @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        Toast.makeText(this, "On Pause Called In First Activity", Toast.LENGTH_LONG).show();
        Log.i("FirstActivity", "Inside onPause");

    }


   @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
       
        Toast.makeText(this, "On Stop Called In First Activity", Toast.LENGTH_LONG).show();
        Log.i("FirstActivity", "Inside onStop");

    }


   @Override
    protected void onDestroy()
    {
        // TODO Auto-generated method stub
        super.onDestroy();
        Toast.makeText(this, "On Destroy Called In First Activity", Toast.LENGTH_LONG).show();
        Log.i("FirstActivity", "Inside onDestroy");

       
    }
   
    public void startSecondActivity(View V)
    {
        // create an new Intent and Start Second Activity
        Intent intent=new Intent(this,SecondActivity.class);
        startActivity(intent);
    }
}
   
   

SecondActivity.java:



public class SecondActivity extends Activity
{
    /** Called when the activity is first created. */


    @Override
    public void onCreate(Bundle savedInstanceState)
    {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout2);
            Toast.makeText(this, "On Create Called In Second Activity", Toast.LENGTH_LONG).show();
            Log.i("SecondActivity", "Inside onCreate");
           
    }
     
   
    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        Toast.makeText(this, "On Start Called In Second Activity", Toast.LENGTH_LONG).show();
        Log.i("SecondActivity", "Inside onStart");
    }
   
   
    @Override
    protected void onResume()
    {
        // TODO Auto-generated method stub
        super.onResume();
       
        Toast.makeText(this, "On Resume Called In Second Activity", Toast.LENGTH_LONG).show();
        Log.i("SecondActivity", "Inside onResume");
    }
   
   
   
 @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        Toast.makeText(this, "On Pause Called In Second Activity", Toast.LENGTH_LONG).show();
        Log.i("SecondActivity", "Inside onPause");
    }


   @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
       
        Toast.makeText(this, "On Stop Called In Second Activity", Toast.LENGTH_LONG).show();
        Log.i("SecondActivity", "Inside onStop");
    }


   @Override
    protected void onDestroy()
    {
        // TODO Auto-generated method stub
        super.onDestroy();
        Toast.makeText(this, "On Destroy Called In Second Activity", Toast.LENGTH_LONG).show();
        Log.i("SecondActivity", "Inside onDestroy");
       
    }
   
   
}



Logs:


03-14 15:33:31.313: I/FirstActivity(22609): Inside onCreate
03-14 15:33:31.318: I/FirstActivity(22609): Inside onStart
03-14 15:33:31.323: I/FirstActivity(22609): Inside onResume
03-14 15:33:44.183: I/FirstActivity(22609): Inside onPause
03-14 15:33:44.313: I/SecondActivity(22609): Inside onCreate
03-14 15:33:44.318: I/SecondActivity(22609): Inside onStart
03-14 15:33:44.323: I/SecondActivity(22609): Inside onResume
03-14 15:33:44.513: I/FirstActivity(22609): Inside onStop
03-14 15:34:03.578: I/SecondActivity(22609): Inside onPause
03-14 15:34:03.603: I/FirstActivity(22609): Inside onStart
03-14 15:34:03.603: I/FirstActivity(22609): Inside onResume
03-14 15:34:03.813: I/SecondActivity(22609): Inside onStop
03-14 15:34:03.818: I/SecondActivity(22609): Inside onDestroy


2 comments:

  1. A work mate referred me to this site. Thnx for the
    resources.

    Visit my page :: http://www.beingaccountable.com.au

    ReplyDelete
  2. it was so good to read and useful to improve my knowledge as updated one, keep writing.

    ReplyDelete