Android Application Components

0
1275

Application components are the ones which when combined together, offers you a brilliant Android application. So, these components exactly act as the building blocks of an Android application. The information regarding all the application components is provided in the manifest file, which is AndroidManifest.xml. This file will help you in understanding the use of each and every application component and how do they interact with each other.

Also Read: Android Architecture

Well, there are 4 major application components that every Android developer should know about. Take a look at the below-formulated table to understand these four components and their uses.

S.No. Application Components Description
1 Activities * Dictates user interface
* Handles user interaction with the screen
2 Services * Handles the running processes in the background of an application
3 Broadcast Receivers * Handles interaction between the operating system (Android) and various applications
4 Content Providers * Handles all the issues related to the data and its management (database management)

Here, we will take a closer look at all the mentioned application components.

Activities
Activities components let the user perform an action on the screen. In other words, a representation of a user interface with an activity being performed on the screen can define this application component. To understand this more clearly, take a look at this example. So, for an instance, a contacts application is a bundle of several activities which includes an activity to add a new contact in your contact list, another activity lets you edit the existing contacts, while other activity would let the user read all the contacts in the contacts application. In such a case, where a single activity is wrapped up with different activities then one of the activities is labeled as the presenter of the application. This means that whenever the application will be launched, you will be presented with the presenter-labeled activity.

To implement any activity, you need to define it as the subclass of Activity class. You need to use the following syntax to implement any activity. This can be done with the use of keyword “extends”.
public class MainActivity extends Activity
{
       ///
}

Services
As mentioned earlier, this component is responsible for handling the time taking operations which generally runs in the background of the operating system (in this case, Android). The simple example of the service component is that when you play music on your mobile phone, you will be able to use other applications too. In this scenario, the music application is running in the background with the music playing operation. Sometimes, you tend to use applications which fetches data from an online server without hindering your other activity on the mobile device.

To implement service component, you need to extend Service class. Below is the syntax to implement a service as a subclass.
public class MainService extends Service
{
      ///
}

Broadcast Receivers
This component takes care of the broadcast messages sent by either applications or the operating system. Broadcast receivers are the one which responds to these messages. For instance, any application which is downloading a file from the online server can broadcast a message stating that some data is downloading and it will soon be available to use by the other applications. This broadcast message will be intercepted by the Broadcast Receiver which will take an action accordingly.

The syntax to implement Broadcast Receiver is a bit different in comparison to other application components as you need to use the onReceive() function while defining this component.
public class MainReceiver extends BroadcastReceiver
{
        public void onReceive(context, intent)
       {
             ///
        }
        ///
}

Content Providers
The work of the Content Providers component is to supply data from one application to another whenever it is requested. To handle these requests, you are provided with ContentResolver class. The methods of this class are capable of handling numerous kinds of requests. The processed data is then stored in the device. It can be stored in the file system, or in a database, or on a cloud, or in some other application.

To implement the component, you will be required to extend the ContentProvider class. So, a content provider component will be defined as the subclass of the ContentProvider class. In here, if you want applications to perform transactions (by the use of onCreate() method) then you must define the standard set of APIs in this subclass.
public class MyContentProvider extends ContentProvider
{
      public void onCreate()
     {
          ///
     }
     ///
}

A Few Other Application Components
There are a few other application components that you should be aware of. These application components include fragments, views, layouts, intents, resources, and manifest. All of these components are used for the creation of above components.

S.No. Application Components Description
1 Fragments * Represents the fragments of a user interface in the Activity component
2 Views * Includes the user interface elements like buttons, drop-down lists, etc.
3 Layouts * Controls the screen format based on different hierarchies of the views
* Takes care of the appearance of the views on the screen
4 Intents * Wires the messages of different components together
5 Resources * Includes external elements like drawable or editable pictures, strings, and constants
6 Manifest * Carries the information regarding the applications
* Configuration file