Android Components Introduction


Android Components are the building blocks of an Android app.

Android has around 5 types of components:

  1. Activity
  2. Service
  3. BroadcastReceiver
  4. ContentProviders
  5. Application.

Each of these components plays a specific role in an Android app which serves a distinct purpose and has distinct life-cycles(the flow of how and when the component is created and destroyed).

Activities are responsible for user interface. Normally you start by creating and activity, or the IDE generating for you a MainActivity.

Services implement long running operations in the background.

BroadcastReceivers listen for System events.

ContentProviders on the other hand are used to store the application data.

All the above have to be registered in the AndroidManifest.xml file.

The above components get tied up together by use of Intents.

Application is also an android component albeit one that is rarely used. Applications provide and maintain a global state to all the stateful components like Activities and Services.

1. Activity

This is the most commonly used android component.

Activities represent and manage user interfaces.

Normally you use subclasses of the Activity class.

public class MainActivity extends Activity{}

One application can have several activities to represent different functional screens.

However, at any one time your application can only display one activity.

Android provides several lifecycle callbacks to handle the varying activity states.

Activities are meant to handle UI related tasks. Normally these tasks take only a short amount of time and all of them can comfortaby be run on the main thread.

However, for long running operations such as networking calls, it is preferable to implement a service to handle them. Though it's possible to use a separate thread within the context of your Activity, it is preferable to delegate such tasks to a service.

This is because activities are what users directly interact with. They can start and stop them at will without necessarily the background task being over.

2. Service

Services implement long running operations in android.

They are the second most commonly used component after the activity.

Services don't involve user interfaces, so they are suitable for performing long running background tasks.

All android components, Services included, do run on the main thread.

Therefore those long running background tasks still have to be executed in a separate thread.

The most commonly used abstractions for the Thread class are AsyncTask and Handler.

Services ensure that your tasks can complete even if the user presses the Home or Back button, unlike activities.

This is because services aren't attached to the user interfaces, hence will continue running even if the activity is not visible.

public class MyService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

3. BroadcastReceiver

This is the third android component.

BroadcastReceiver listen for system events.

Unlike activities and services, BroadcastReceivers are stateless. This implies the BroadcastReceiver object is only valid during the invocation of the onReceive() method.

Thus you cannot keep a reference to a BroadcastReceiver object declared inside the manifest anywhere in your code.

You cannot also add it's objecr as a listener or callback to an asynchronous operation.

Within the onReceive() method, you should delegate the call to another component, via the Context.startActivity() for activities and Context.startService() for service.

BroadcastReceiver therefore are only majorly used for listening to System events.

The Android SDK already predefines dozens of BroadcastIntents.

BroadcastReceivers have the ability to be programmatically declared inside a Service or Activity.

After the declaration they are then manually registered and unregustered to release memory.

4. ContentProvider

ContentProvider store application data.

They are unique in that you don't have to declare them to store data.

ContentProviders allow us share data between applications.

In some situations they can also be easier to use especially when working with AdapterViews.

5. Application

Application is the top level component for android.

It get's created before all the other components.

It also gets killed last.

Therefore it's normally used to maintain a global state across the different components.

By default all android applications have the application component.

The reference to the application component can be retrieved through a call of Context.getApplication().

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think

Dear readers drop us your comments below. We are building a community of students and learners. Start by dropping us your suggestions below. What tutorials do you want us to do for example? Where can we improve? What are some awesome resources out there? Do you have any code you want to share with us?
By the way that example or snippet you have lying in your computer can really help beginner programmers. We can share it here with other students.

Previous Post Next Post