The Activity Element (And Its Children)

The children of <application> mostly represent the “table of contents” for the app.

Android has four major types of “components”:

Most of these will be registered in the manifest via corresponding child elements of <application>:

Component Element
Activity <activity>
Service <service>
Content Provider <provider>
Broadcast Receiver <receiver>

Your app may have several of one type, such as having several activities. Your app may have none of a particular type, such as having no broadcast receivers registered in the manifest.

Our starter app has a single <activity> element, and nothing more:

    <activity
      android:name=".MainActivity"
      android:exported="true">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>

<activity> elements have an android:name attribute. This will identify the Java or Kotlin class that contains the implementation of the activity. The android:name attribute, in this case, has a bare Java class name prefixed with a single dot (.MainActivity). Sometimes, you will see android:name with a fully-qualified class name (e.g., com.commonsware.helloworld.MainActivity). Sometimes, you will see just a bare Java class name (e.g., MainActivity). Both MainActivity and .MainActivity refer to a Java class that will be in your project’s package — the one you declared in the package attribute of the <manifest> element.

Sometimes, an <activity> element will have an <intent-filter> child element describing under what conditions this activity will be displayed. Most apps will have at least one <activity> element that sets up your activity to appear in the launcher, so users can choose to run it. That is what this <intent-filter> element does, though the details of how that works are beyond the scope of this particular book. Suffice it to say that whenever you see an <activity> element with this particular <intent-filter> (an <action> of android.intent.action.MAIN and a <category> of android.intent.category.LAUNCHER), you know that this activity should appear in the launcher for the user to be able to start.

The other component elements — <service>, <provider>, <receiver> — will have similar characteristics:


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.