Android Activity
An Activity represents a single screen with a user interface, similar to a window or frame in Java. Android activities are subclasses of the ContextThemeWrapper class.
If you have programmed with C, C++, or Java, you might know that programs start with the main() function. Similarly, Android systems initialize their programs by invoking the onCreate() callback within an activity. There is a sequence of callback methods to start an activity, and a corresponding sequence to close an activity, as illustrated in the activity lifecycle diagram below:
Activity class defines the following callbacks. You don't need to implement all the callback methods. However, it's important to understand each one to ensure your app behaves as users expect.
Callback | Description |
---|---|
onCreate() | This is the first callback and is called when the activity is first created. |
onStart() | This callback is called when the activity becomes visible to the user. |
onResume() | This callback is called when the activity starts interacting with the user. |
onPause() | The paused activity does not receive user input and cannot execute any code. This is called when the current activity is being paused and the previous activity is being resumed. |
onStop() | This callback is called when the activity is no longer visible. |
onDestroy() | This callback is called before the activity is destroyed by the system. |
onRestart() | This callback is called when the activity is stopped and then started again. |
Example
This example demonstrates the lifecycle of an Android application activity through simple steps. Follow the steps below to modify the Android application created in the Hello World example section.
Step | Description |
---|---|
1 | Create an Android application using the Eclipse IDE and name it HelloWorld under the com.example.helloworld package, as described in the previous Hello World Example section. |
2 | Modify the main activity file MainActivity.java as shown below. Keep the other parts unchanged. |
3 | Run the application to open the Android emulator and check the results of the application modifications. |
Here is the modified content of the main activity file src/com.example.helloworld/MainActivity.java, which includes each basic lifecycle method. The Log.d() method is used to generate log messages:
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");
}
/** Called when the activity is about to become visible. */
@Override
protected void onStart() {
super.onStart();
Log.d(msg, "The onStart() event");
}
/** Called when the activity has become 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 just before the activity is destroyed. */
@Override
public void onDestroy() {
super.onDestroy();
Log.d(msg, "The onDestroy() event");
}
}
The Activity class loads all UI components from the XML file 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 multiple activities without any restrictions. Each activity defined for the application must be declared in AndroidManifest.xml. The main activity of the application needs to be declared in the manifest, and its intent filter tag must include the MAIN action and LAUNCHER category, as shown below:
<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 neither the MAIN action nor the LAUNCHER category is declared in the activity, the application's icon will not appear in the main screen's app list.
Let's run the modified "Hello World!" application. Assume you have created an AVD during the environment setup. To run the application from Eclipse, open the activity file in the project and click the run button on the toolbar.
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
Now, let's click the red button on the Android emulator.
07-19 15:01:10.995: D/Android :(866): The onPause() event
07-19 15:01:12.705: D/Android :(866): The onStop() event
Let's click the menu button on the Android emulator again.
07-19 15:01:13.995: D/Android :(866): The onStart() event
07-19 15:01:14.705: D/Android :(866): The onResume() event
Next, let's click the back button on the Android emulator.
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