Android Application Components Explanation

In this tutorial, we will learn about different android application components. Getting familiar with these android application components helps us to know the project structure better.
Android application components are basic, but essential, building blocks of any android application. Each component is an entry point through which the system or user can enter your app. Some components depend on other. They are loosely coupled with Application manifest file. In Manifest file, we describe how are they being used and interacting with other components. Each component type has a distinct purpose and distinct lifecycle that describes how the component was created and destroyed.

There are 4 main android application components. They are

1. Activities
2. Services
3. Broadcast Receivers
4. Content Providers

1. Activities

An activity is single screen with user interface. For example, A activity can have user interface for signup process, Another activity can have user interface for login process. Another activity can show all the emails, like gmail app showing all the mails in activity. All activity are independent from each other i.e. they do not depend on each other to show something. They are standalone. However, one activity can start another activity when required. But, it does not mean they are depended to the caller activity. They can be called from anywhere. It also represents entry point for any android application. An activity provides following features

1. It keeps track of the what the user currently using..i.e. what is on the screen right now. Activity does so that system keeps running the process that activity needs to be on the screen.
2. It keeps track of the previously visited activities or other things so that user may return to it whenever needed. It also store the state of the activity before closing the activity so that it can restore the state whenever needed.
3. It also provides a way for apps to implement user flows between each other, and for the system to coordinate these flows.

If any application has more than one activity, one of them is made launcher activity. Launcher activity is launched when application is started.

An activity is implemented as subclass of Activity class. You can create an activity as below

public class MainActivity extends Activity {
}

2. Services

A service is component that runs in background to perform long running operations or to perform work for remote processes. For example, A service may play music in the background while the use is using a different application. A service may upload file to server from the server while the use is using a different application. A service does not provide user interface. You can declare the service as below.

public class MyService extends Service {
}

3. Broadcast Receivers

A broadcast receiver is a component that receive information, broadcasted by other applications or by the system, and take some actions accordingly. For example, An app can schedule an alarm to notify the user at any specified time. It’s same as we set an alarm to notify at a specific time. Another example is notify the user when battery is low. Although the user do not show user interface, it can create status bar notifications to alert the user to take a specific action.
A broadcast receiver is implemented as subclass of BroadcastReceiver.

public class MyReceiver  extends  BroadcastReceiver {
   public void onReceive(context,intent){}
}

4. Content Providers

As the name depicts, Content providers make the content of any file system, SQLite database, or on any other persistent storage, available to any other applications. i.e. you can use content providers to get a specific set of data in any application. Means it acts as bridge between the data and the application. you can also access many native databases using content providers. For example, You can access contacts using ContactManager. You must have proper permissions to access data of other applications.
You can implement Content Providers by subclassing ContentProvider as below –

public class MyProvider extends  ContentProvider {
   public void onCreate(){}
}



Additional Android Application Components

There are some additional components as well that are used with above fundamental components. They are –

A. Fragments
B. Views
C. Layouts
D. Intents
E. Manifest
F. Resources

A. Fragments

A fragment is a part of user interface in a activity. You can use one or more fragments in an activity. You can assume fragment as a modular part of activity that has it’s own life cycle, can receive it’s own input, can be added or removed dynamically, while activity is running. A Fragment must always be embedded in activity because it’s life cycle is affected by host activity life cycle. i.e. you can use fragment without activity.

B. Views

This is basic building block for user interface component. It is rectangular area on the screen. We can draw or handle some events in this rectangular area. Also, View is base class for widgets( used to create ui components).

C. Layouts

A layout is visual structure for any user interface. For example, UI for activity, widgets, fragments etc. You can declare layouts in 2 ways.
1. Statically: You can declare layouts in xml file. xml file is added in res => layout folder of any android application.
2. Dynamically: You can declare layouts at runtime as well. To create layouts at runtime, you would need to add related code in java file.

D. Intents

Intent is an abstract information related to an operation to be performed. Intent is used to communicate between different android components in several ways. Some of them are –
(i) Start an Activity: You can use intent to start an activity from another activity or fragment class.
(ii) Start a Service: Same as Activity, You can use intent to start an service from a activity or fragment class.
(iii) Broadcasting a message: You can use intent to broadcast a message so that proper action is taken against the message broadcasted.

E. Manifest

Every android application must have AndroidManifest.xml file in it’s root directory. It contains a short description of the android application. For example, It contains the package name of the application(that acts as unique identifier of the application), informations about the components of the application(Activities, services, broadcast receivers, content providers), permissions needed to run the application, minimum level of api that application need to run etc.

F. Resources

There are many more things you need while creating UI for an activity in android application. For example, You may need images, icons, animations, audio, video etc. while creating user interface. They are the resources we need to store somewhere in the application. Android has a separate and structured folder for these resources. They are stored in res folder in any android application.

Conclusion

we use different android application components while creating any android application. Some of them are Activity, services, content provider etc. There are some additional components as well. We have tried to explain each component clearly. If you find any difficulty, please let me know in comment section.

Leave a Reply