ACTIVITIES

In Android, an activity is a window that contains the user interface for your application, and users interact directly with the activities of your applications.

image

NOTE If you are new to Android programming, I suggest you read my book Beginning Android Application Development (also from Wrox, 2011) to get acquainted with the basic concepts of activities and intents.

To create an activity, you create a Java class that extends the Activity base class:

package net.learn2develop.Activities;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Your activity class would then load its UI component using the XML file defined in your res/layout folder of the project. In this example, you would load the UI from the main.xml file:

setContentView(R.layout.main);

Every activity you have in your application must be declared in your AndroidManifest.xml file, like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="net.learn2develop.Activities"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon"
        android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                 <category
                    android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="11" />
</manifest>

The Activity base class defines a series of events that governs the life cycle of an activity. The Activity class defines the following events:

  • onCreate() — Called when the activity is first created
  • onStart() — Called when the activity becomes visible to the user
  • onResume() — Called when the activity starts interacting with the user
  • onPause() — Called when the current activity is being paused and the previous activity is being resumed
  • onStop() — Called when the activity is no longer visible to the user
  • onDestroy() — Called before the activity is destroyed by the system (either manually or by the system) to conserve memory
  • onRestart() — Called when the activity has been stopped and is restarting again

By default, the activity created for you contains the onCreate() event. Within this event handler is the code that helps to display the UI elements of your screen.

Figure 2-1 shows the life cycle of an activity and the various stages it goes through — from when the activity is started until it ends.

FIGURE 2-1

Image reproduced from work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License. See http://developer.android.com/reference/android/app/Activity.html

image

The best way to understand the various stages of an activity is to create a new project, implement the various events, and then subject the activity to various user interactions.

TRY IT OUT: Exploring the Life Cycle of an Activity

codefile Activities.zip available for download at Wrox.com

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

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. The activity is shown in Figure 2-3.

4. When the activity is first loaded, you should see the following in the LogCat window (Window ⇒ Show View ⇒ LogCat; see also Figure 2-4):

04-03 01:05:01.256: DEBUG/Events(1222): In the onCreate() event
04-03 01:05:01.256: DEBUG/Events(1222): In the onStart() event
04-03 01:05:01.276: DEBUG/Events(1222): In the onResume() event

5. When you now click the Back button on the Android emulator, observe that the following is printed:

04-03 01:07:15.785: DEBUG/Events(1222): In the onPause() event
04-03 01:07:17.335: DEBUG/Events(1222): In the onStop() event
04-03 01:07:17.335: DEBUG/Events(1222): In the onDestroy() event

6. Click the Recent Apps button (located on the System Bar at the bottom of the screen) and then click the Activities icon (see Figure 2-5). Observe the following printed in the LogCat window:

04-03 01:08:42.446: DEBUG/Events(1222): In the onCreate() event
04-03 01:08:42.446: DEBUG/Events(1222): In the onStart() event
04-03 01:08:42.466: DEBUG/Events(1222): In the onResume() event

7. Click the Home button on the Android emulator so that the activity is pushed to the background. Observe the output in the LogCat window:

04-03 01:12:54.945: DEBUG/Events(1222): In the onPause() event
04-03 01:12:57.106: DEBUG/Events(1222): In the onStop() event

8. Notice that the onDestroy() event is not called, indicating that the activity is still in memory. Click the Apps button and launch the Activities application once more. The activity is now visible again. Observe the output in the LogCat window:

04-03 01:18:06.855: DEBUG/Events(1222): In the onRestart() event
04-03 01:18:06.855: DEBUG/Events(1222): In the onStart() event
04-03 01:18:06.865: DEBUG/Events(1222): In the onResume() event

The onRestart() event is now fired, 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. 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.

image

NOTE Even if an application has only one activity and the activity is killed, the application will still be running in memory.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset