Android Application Components
Application components are the fundamental building blocks of an Android application. These components are loosely organized by the application manifest file. AndroidManifest.xml describes each component of the application and how they interact.
The following are the four main components that can be used in an Android application.
| Component | Description |
|---|---|
| Activities | Describes the UI and handles the interaction between the user and the machine screen. |
| Services | Handles background operations associated with the application. |
| Broadcast Receivers | Handles communication between the Android operating system and the application. |
| Content Providers | Handles issues related to data and database management. |
Activities
An activity identifies a single screen with a user interface. For example, an email application can contain an activity for displaying a list of new emails, another activity for composing an email, and yet another activity for reading an email. When an application has more than one activity, one of them is marked as the one displayed when the application is launched.
An activity isActivitya subclass of the class, as shown below:
public class MainActivity extends Activity {
}
Services
A service is a component that runs in the background to perform long-running operations. For example, a service can play music in the background while the user is using a different program, or fetch data over the network in an activity without blocking user interaction.
A service isServicea subclass of the class, as shown below:
public class MyService extends Service {
}
Broadcast Receivers
A broadcast receiver simply responds to broadcast messages sent from other applications or the system. For example, an application can initiate a broadcast to let other applications know that some data has been downloaded to the device and is available for them to use. Therefore, the broadcast receiver intercepts these communications and takes appropriate action.
A broadcast receiver isBroadcastReceivera subclass of the class, and each message is broadcast in the form ofIntentan object.
public class MyReceiver extends BroadcastReceiver {
}
Content Providers
The content provider component provides data from one application to another application through requests. These requests areContentResolverhandled by methods of the class. This data can be stored in the file system, a database, or elsewhere.
A content provider isContentProvidera subclass of the class, and it implements a set of standard APIs for other applications to perform transactions.
public class MyContentProvider extends ContentProvider {
}
We will cover the application components in separate chapters with the details of these tags.
Supplementary Components
There are some supplementary components used to construct the entities mentioned above, the logic between them, and the connections between them. These components are as follows:
| Component | Description |
|---|---|
| Fragments | Represents a behavior in an activity or a part of the user interface. |
| Views | UI elements drawn on the screen, including buttons, lists, etc. |
| Layouts | An inheritance of View that controls the screen layout and displays the appearance of views. |
| Intents | Message connections between components. |
| Resources | External elements, such as string resources, constant resources, and image resources. |
| Manifest | The application's configuration file. |