Pages

Ads 468x60px

Monday 1 July 2013

Practice: Life Cycle of an Android Activity

Practice: Life Cycle of an Android Activity

1. Using Eclipse, create a new Android project and name it as shown in Figure Below.

2.   In the MainActivity.java file, add the following statements in bold:
     
        package net.learn2develop.Activities;
        import android.app.Activity;
        import android.os.Bundle;
        import android.util.Log;
  
public class MainActivity extends Activity
  {
         String tag = “Events”;
         /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Log.d(tag, “In the onCreate() event”);
       }
     public void onStart()
     {
         super.onStart();
        Log.d(tag, “In the onStart() event”);
     }
    public void onRestart()
    {
            super.onRestart();
             Log.d(tag, “In the onRestart() event”);
     }
  public void onResume()
  {
        super.onResume();
        Log.d(tag, “In the onResume() event”);
   }
   public void onPause()
   {
        super.onPause();
        Log.d(tag, “In the onPause() event”);
   }
    public void onStop()
    {
        super.onStop();
        Log.d(tag, “In the onStop() event”);
     }
     public void onDestroy()
    {
            super.onDestroy();
           Log.d(tag, “In the onDestroy() event”);
     }
}

3.   Press F11 to debug the application on the Android Emulator.
4.   When the activity is first loaded, you should see the following in the LogCat window (click on the Debug perspective; see also Figure below :

12-28 13:45:28.115: DEBUG/Events(334):      In the onCreate() event
12-28 13:45:28.115: DEBUG/Events(334):      In the onStart() event
12-28 13:45:28.115: DEBUG/Events(334):      In the onResume() event



5 When you now press the back button on the Android Emulator, observe that the following is printed:

       12-28 13:59:46.266: DEBUG/Events(334): In the onPause() event
       12-28 13:59:46.806: DEBUG/Events(334): In the onStop() event
       12-28 13:59:46.806: DEBUG/Events(334): In the onDestroy() event
6  Click the Home button and hold it there. Click the Activities icon and observe the following:

         12-28 14:00:54.115: DEBUG/Events(334): In the onCreate() event
         12-28 14:00:54.156: DEBUG/Events(334): In the onStart() event
         12-28 14:00:54.156: DEBUG/Events(334): In the onResume() event

7 Press the Phone button on the Android Emulator so that the activity is pushed to the background. Observe the output in the LogCat window:

       12-28 14:01:16.515: DEBUG/Events(334): In the onPause() event
       12-28 14:01:17.135: DEBUG/Events(334): In the onStop() event

8 Notice that the onDestroy() event is not called, indicating that the activity is still in memory. Exit the phone dialer by pressing the Back button. The activity is now visible again. Observe the output in the LogCat window:

         12-28 14:02:17.255: DEBUG/Events(334): In the onRestart() event
         12-28 14:02:17.255: DEBUG/Events(334): In the onStart() event
         12-28 14:02:17.255: DEBUG/Events(334): In the onResume() event

The onRestart() event is now fi red, followed by the onStart() and onResume() events.
How It Works As you can see from this simple experiment, an activity is destroyed when you press the Back button. 

This is crucial to know, as whatever state the activity is currently in will be lost; hence, you need to write additional code in your activity to preserve its state when it is destroyed (Next Lesson  will show you how).

At this point, note that the onPause() event is called in both scenarios — when an activity is sent to the background, as well as when it is killed when the user presses the Back button.
When an activity is started, the onStart() and onResume() events are always called, regardless of whether the activity is restored from the background or newly created.

0 التعليقات:

Post a Comment

.

Education blog