Activity in Android - Part 1

Merriam Webster dictionary defines activity as

the quality or state of being active : behavior or actions of a particular kind

In a nutshell, Activity refers to place of interaction with an Android device. Technically, it represents a Window, but the Android takes care of creating the window for us, eliminating the need for direct interaction with it's methods.

This post will focus exclusively on discussing the activity and its lifecycle. It is assumed that you already have a basic understanding of writing Android applications, and it is not intended as an introductory guide to Android development.

Size of an Activity

In Android, an application can contain multiple activities. It is not necessary for an activity to occupy the full screen. It can be displayed as a floating window, embedded into another window, or used in multi-window mode. Therefore, careful consideration is required when deciding whether to create a new activity or not. Android's official definition serves as a guide in this context.

An activity is a single, focused thing that the user can do.

In other words, if your activity is like a floating window, all the actions you want the user to perform within that window should be part of the same activity, without requiring them to switch to another window.

Definition of an Activity

In Android, we define an Activity rather than create it. The creation and management of an Activity are handled by the Android system itself. Unlike in java there is no public static void main(String[] args) function inside Activity class. It is actually defined inside the ActivityThread. The reason is that there is a lot of activity happening in the background to make your activity visible and interactive. Therefore, all those parts are abstracted away by Android Frameworks.

For end users, if they want to create an activity, they have to define it in two places.

1. Registration in manifest

As you may be aware, AndroidManifest.xml is a crucial file in any Android project. It serves as a manifesto for your application, informing the Android framework about its various aspects. This file can be accessed by anyone who has the APK file, and Android OS heavily relies on it to understand the details of your application. You can also check the same with following command

aapt dump badging <you-apk-name>.apk

The AndroidManifest is also where you define certain attributes of your activity (lower case). Below is one example of the Compass application from Android samples

<!-- This file describes the code in the Compass package, which is
     used by the system to determine how to start your application and
     integrate it with the rest of the system.  -->

<!-- Declare the contents of this Android application.  The namespace
     attribute brings in the Android platform namespace, and the package
     supplies a unique name for the application.  When writing your
     own application, the package name must be changed from "com.example.*"
     to come from a domain that you own or have control over. -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.compass">

    <!-- This package contains an application...  The 'label' is the name
         to display to the user for the overall application, and provides
         a default label for all following components.  The syntax here is a
         reference to one of our string resources.-->
    <application android:label="@string/compass_app">

        <!-- An Activity in the application - this is something the user
             can launch and interact with.  The "name" attribute is the
             name of the class within your package that implements this
             activity. -->
        <activity android:name="CompassActivity">

            <!-- An IntentFilter tells the system when it should use your
                 activity.  This allows the user to get to your activity
                 without someone having to explicitly know to launch your
                 class "com.example.android.compass_app.CompassActivity". -->
            <intent-filter>
                <!-- The MAIN action describes a main entry point into an
                     activity, without any associated data. -->
                <action android:name="android.intent.action.MAIN" />

                <!-- This places this activity into the main app list. -->
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Here, we are declaring CompassActivity in our application manifest. The intent-filter is used to instruct Android to display our activity in the application list that the user sees. If you remove this intent filter, your activity will no longer be visible in the app list, even if your APK is installed.

2. Class definition

In the first part, we declared in the manifest that there is a CompassActivity class in the package com. example. android. compass. Let's explore same.

An Activity, an android always extends the Activity class. There are a few derived classes that internally extend the Activity class but provide additional methods to support specific use cases. Depending on the requirements, you can choose to extend any of those as well, eg FragmentActivity, AppCompatActivity, PreferenceActivity (deprecated in API level 29).

In the sample code, CompassActivity is defined like below.

package com.example.android.compass;
// import statements
public class CompassActivity extends Activity implements Renderer, SensorEventListener {
    // implementations
}

Now that we understand how to define an Activity, let's go over how Android loads your layout in the window in the next part.