Android Activity
Android Activity
An Activity represents a single screen with a user interface, such as a window or frame in Java. Android Activities are subclasses of the ContextThemeWrapper class.
If you have programmed using C, C++, or Java, you should know that these programs start from the main() function. Similarly, the Android system initializes its program through the onCreate() callback in the Activity. There is a sequence of callback methods to start an Activity, and a sequence of methods to close an Activity, as shown in the Activity lifecycle diagram below:

The Activity class defines the following callbacks. You do not have to implement all the callback methods. However, understanding each one is very important, as implementing them ensures that your application behaves as the user expects.
| Callback | Description |
|---|---|
| onCreate() | This is the first callback, called when the activity is first created |
| onStart() | This callback is called when the activity is visible to the user |
| onResume() | This callback is called when the application starts to be interactive with the user |
| onPause() | Called when the current activity is about to be paused and the previous activity is about to be resumed, and the paused activity cannot accept user input or execute any code |
| onStop() | Call when the activity is not visible |
| onDestroy() | Call before the activity is destroyed by the system |
| onRestart() | Call when the activity is reopened after being stopped |
Instance
This instance demonstrates the lifecycle of an Android application activity through simple steps. Modify the Android application we created in the Hello World instance chapter according to the following steps.
| Steps | Description |
|---|---|
| 1 | Create an Android application named HelloWorld in the com.example.helloworld package using Eclipse IDE, as introduced in the Hello World Example chapter. |
| 2 | Modify the main activity file MainActivity.java as shown below. Keep other parts unchanged. |
| 3 | Run the application to open the Android emulator and check the results of the modifications. |
The changes to the main activity file src/com.example.helloworld/MainActivity.java are below. It includes every basic lifecycle method. The Log.d() method is used to generate log information:
Original prompt (Chinese, left untranslated)
package com.example.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
public class MainActivity extends Activity {
String msg = "Android : ";
/** Called when the activity is first created */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(msg, "The onCreate() event");
}
/** Call when the activity is about to become visible */
@Override
protected void onStart() {
super.onStart();
Log.d(msg, "The onStart() event");
}
/** Call when the activity is visible */
@Override
protected void onResume() {
super.onResume();
Log.d(msg, "The onResume() event");
}
/** Called when another activity gains focus */
@Override
protected void onPause() {
super.onPause();
Log.d(msg, "The onPause() event");
}
/** Called when the activity is no longer visible */
@Override
protected void onStop() {
super.onStop();
Log.d(msg, "The onStop() event");
}
/** Called when the activity is destroyed */
@Override
public void onDestroy() {
super.onDestroy();
Log.d(msg, "The onDestroy() event");
}
}
Activity classes load all UI components from XML files in the project's res/layout. The following statement loads UI components from res/layout/activity_main.xml:
setContentView(R.layout.activity_main);
An application can have one or more activities, with no restrictions. Every activity defined for the application must be declared in AndroidManifest.xml. The main activity of the application must be declared in the manifest and must include the MAIN action and LAUNCHER category in the intent-filter tag. For example:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="22" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
If the MAIN action or LAUNCHER category is not declared in the activity, the application icon will not appear in the application list on the home screen.
Let's run the modified "Hello World!" application. Assume you have already created an AVD during environment setup. Run the application from Eclipse, open an activity file in a project, and click the run icon on the toolbar.
Eclipse installs and launches the application on the AVD. If everything goes well, the simulator screen will display as shown below, and you can see the log information in the LogCat window of the Eclipse IDE:
07-19 15:00:43.405: D/Android :(866): The onCreate() event
07-19 15:00:43.405: D/Android :(866): The onStart() event
07-19 15:00:43.415: D/Android :(866): The onResume() event
Let's click the red button on the Android emulator
, which will generate the following event message in the LogCat window of the Eclipse IDE:
<code>07-19 15:01:10.995: D/Android :(866): The onPause() event
07-19 15:01:12.705: D/Android :(866): The onStop() event
</code>
Let's click the menu button on the Android emulator again
, it will generate the following event message in the LogCat window of the Eclipse IDE:
<code>07-19 15:01:13.995: D/Android :(866): The onStart() event
07-19 15:01:14.705: D/Android :(866): The onResume() event
</code>
Next, let's click the back button on the Android emulator
, it will generate the following event message in the LogCat window of the Eclipse IDE, completing the entire lifecycle of the Android application.
07-19 15:33:15.687: D/Android :(992): The onPause() event
07-19 15:33:15.525: D/Android :(992): The onStop() event
07-19 15:33:15.525: D/Android :(992): The onDestroy() event