YouTip LogoYouTip

Android Acitivities

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 ever programmed in C, C++, or Java, you know that these programs start from a `main()` function. Similarly, the Android system initializes its program by calling the `onCreate()` callback in an activity. There is a sequence of callback methods to start an activity, and a sequence of methods to close it, as shown in the activity lifecycle diagram below: ![Image 1: Picture](#) The Activity class defines the following callbacks. You don't need to implement all of the callback methods. However, it's important to understand each of them and implement them to ensure your app behaves as users expect. | Callback | Description | | --- | --- | | onCreate() | This is the first callback, 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 application starts interacting with the user. | | onPause() | A paused activity cannot accept user input and cannot execute any code. It is called when the current activity is about to be paused and the previous activity is about to be 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 restarted after being stopped. | * * * ## 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 chapter. | Step | Description | | --- | --- | | 1 | Use the Eclipse IDE to create an Android application and name it HelloWorld under the package com.example.helloworld, as introduced in the previous Hello World Example chapter. | | 2 | Modify the main activity file MainActivity.java as shown below. Keep the rest unchanged. | | 3 | Run the application to open the Android emulator and check the modified results of the application. | Below is the modified content of the main activity file `src/com.example.helloworld/MainActivity.java`. It includes every basic lifecycle method. The `Log.d()` method is used to generate log messages: ```java 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 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 about to be 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 the UI components from the `res/layout/activity_main.xml` file: ```java setContentView(R.layout.activity_main); An application can have one or more activities, with no restriction. Each activity defined for the application must be declared in the `AndroidManifest.xml` file. The main activity of the application must be declared in the manifest with an intent filter tag that includes the MAIN action and LAUNCHER category, as shown below: ```xml If either the MAIN action or the LAUNCHER category is not declared in the activity, the application's icon will not appear in the home screen's app list. Let's run the modified "Hello World!" application. Assuming you have created an AVD during environment setup. To run the app from Eclipse, open an activity file in the project and click the Run ![Image 2: Picture](#) icon from the toolbar. Eclipse installs the app on the AVD and starts it. If everything is fine, you will see the emulator screen as shown below, and you can see the log messages in the Eclipse IDE's LogCat window: 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 ![Image 3: Picture](#) on the Android emulator. This will generate the following event messages in the Eclipse IDE's LogCat window: 07-19 15:01:10.995: D/Android :(866): The onPause() event 07-19 15:01:12.705: D/Android :(866): The onStop() event Now, let's click the menu button ![Image 4: Picture](#) on the Android emulator again. This will generate the following event messages in the Eclipse IDE's LogCat window: 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 ![Image 5: Picture](#) on the Android emulator. This will generate the following event messages in the Eclipse IDE's LogCat window, completing the entire lifecycle of the activity in 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
← Python3 Leap YearAndroid Hello World Example β†’