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
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.compass">
<application android:label="@string/compass_app">
<activity android:name="CompassActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<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;
public class CompassActivity extends Activity implements Renderer, SensorEventListener {
}
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.