App components are the essential building blocks of an Android app. Each component is an entry point through which the system or a user can enter app. Some components depend on others.In this post we are going to discuss about the four fundamental building blocks from which all the android applications are built. These building blocks are implemented as Java classes.
There are four major building blocks of Android, let's understand them.
Anything that is visible to the user has an activity. Basically, activity is defined to present the graphical user interface to the user and capture user's interaction through that interface. An activity should support a single focused thing that the user can do. eg: any UI visible in the app.Basically, it is an entry point for interacting with user.
Unlike activities, services don't require any user interface and runs in the background thread. We basically place long running operations in services. They provide a way for different processes to request operations and share data. eg: music application, while we play a song, it is still playing in the background even though the app is closed.We can say, Service is an entry point for keeping an application to run in background.
Broadcast receivers are the fundamental components that listens to and responds to events. Broadcast receiver acts as a subscriber that listens to its required Intents and respond to it. eg: messaging app which shows a notification when a message is received.A broadcast receiver is a component/entry point that enables the system to deliver events to the app outside of a regular user flow, allowing the app to respond to system-wide broadcast announcements.
If an application manages data and needs to expose that data to other applications, we use Content Providers. Content Providers allows applications to store and share data. If an application needs to access data from another application this is done through Content Provider of that application. The access might be read or write or both operations. Similarly, content provider is used to provide data to the same containing application as well. Thus, we can say content provider is a database style component that handles interprocess communication between applications. eg: messaging app which can explore our contacts through contacts application.
You must declare all app components using the following elements:
<activity> elements for activities.
<service> elements for services.
<receiver> elements for broadcast receivers.
<provider> elements for content providers.
Activities, services, and content providers that you include in your source but do not declare in the manifest are not visible to the system and, consequently, can never run. However, broadcast receivers can be either declared in the manifest or created dynamically in code as BroadcastReceiver objects and registered with the system by