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= ...

Get Beginning Android now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.