|
|
ACTIVITY LIFE CYCLE
1. Using Eclipse, create a new Android project .
2. In the MainActivity.java file, add the following statements in bold:
package com.sartaj.Activities; // package name
import android.app.Activity; // Activity Package
import android.os.Bundle; // Bundle Package
import android.util.Log; // Util Package
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):
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 fired, followed by the onStart() and onResume() events.
0 comments:
Post a Comment