- Application components are the essential building blocks of an Android application.
- Each component is a different point through which the system can enter your application. Not all components are actual entry points for the user and some depend on each other, but each one exists as its own entity and plays a specific role.
- There are four different types of application components in android. And they are as follows:
- Activities.
- Services.
- Content Providers.
- Broadcast Receiver
- Android applications have more than one entry point. Whereas, java has only a single entry point. ( main() is the entry point in java)
- Activities:
- An activity represents a single screen with a user interface. For example consider an Email application, the following are called activities.
- Showing list of mails.
- Composing a new mail.
- Reading a mail.
- Each activity is independent of the other activity. And can work together.
- For example, Camera application can start an activity in the e-mail application that composes a new mail in order to share a picture.
- Each one is implemented as a subclass of the Activity base class.
- An application might consist of just a single activity or can contain multiple activities.
- Typically, one activity in an application specified as the main activity, which is presented to the user when an application is launching for the first time.
- Each activity can start another in order to perform some actions. When a new activity started the previous activity is stopped and pushed on to the back stack. The back stack works on LIFO (Last in First Out) mechanism.
- Each activity gives a window to draw its user interface.
- Typically, the window fills the screen, but it might be smaller than the screen and may float on top of other windows.
- Services:
- A service does not have a visual user interface.
- A service is a component that runs in the background to perform long running operations or to perform work for remote processes.
- Example:
- Playing the music while the user is in a different application.
- Fetching the data over the network without disturbance.
- Each service extends the Service base class.
- A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process.
- Broadcast Receivers:
- A broadcast receiver is a component that does nothing but receives and reacts to broadcast announcements.
- Broadcast announcements are of two types.
- System originated.
- Applications initiated.
- System Originated:
- Following are the examples of system originated broadcast announcements.
The screen has turned off.
Battery is low.
Picture has been captured.
The time zone has changed.
- Application initiated:
- Applications can also initiate the broadcast announcements
- Announcing to other applications that some data has been downloaded to the device and is available for them to use.
- A broadcast receiver is just a gate way to other components and intended to do a very minimal amount of work.
- Example: It might initiate a service to perform some work based on an event.
- Content Providers:
- A content Provider makes a specific set of the application's data available to other applications.
- We can store the data in file system, an SQLite Database, on the web or any other persistent storage location your application can access.
- The content provider extends the ContentProvider base class to implement a standard set of methods that enable other applications to retrieve and store data of the type it controls.
- However, applications do not call these methods directly. Rather they use a ContentResolver object and call its methods instead. A ContentResolver can talk to any content provider; it cooperates with the provider to manage any inter process communication that's involved.