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”:
- Activities, representing the UI
- Services, representing background processing that is decoupled from the UI
- Content providers, which expose databases or data streams to other apps or the operating system
- Broadcast receivers, which supply the “subscriber” side of a publish/subscribe messaging system used by apps and the operating system to communicate
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:
- They all will have an
android:name
attribute, identifying the code that serves as the implementation for that component - They might have an
<intent-filter>
- They might have other attributes as well (e.g.,
android:permission
)
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.