Android Application
Components
Introduction
• An android application comprises four essential components and
some additional components.
• The four components serve a specific purpose and have a specific
lifecycle.
• They define a path for both user and system to how they can start
or leave an application.
Core
Components
1. Android Activity
• Activity represents a single screen or view
within an application that provides a user
interface for interacting with the app.
• There can be several such screens in one
application.
• Every activity consists of UI elements that help
the user find out the task he wants to perform.
• Each activity has a specific lifecycle and is
independent of other activities.
Example
• Suppose you see the first activity: the Chats whenever you open
your messaging application(Ex: Whatsapp). Then, when you try to
chat with someone, you move to another activity.
• Another example, suppose you wish to capture a picture and send
it to your friend. For this, you shift from your WhatsApp activity to
Camera Activity. It doesn’t imply that your WhatsApp activity is
destroyed, but it gets paused when capturing the picture.
These all things are managed by the Activity Manager, and it
takes care of which activities to start, pause or destroy.
An activity is implemented as a subclass of class Activity(using java).
public class MainActivity extends
Activity {
}
2. Android Services
• Services are the processes that run in the
background and do not have any user interface for
the user to interact.
• It simply runs in the background and performs the
tasks.
• It doesn’t matter whether the application is active
or not; its services may be active.
• Services are a beneficial component and allow the
user to perform various tasks like music playback,
notification triggering, warnings, etc.
Examples
1. You kept the music on your music application and
then went out of that application. Even yet, the
background music continues to play.
2. Even if your messaging app is not active, you still
receive notifications for any new messages coming
to your device.
For implementing Services in android, you need to create a sub-
class from the “Service” Class. (using kotlin)
class ServiceName : Service()
{
//Your Code
}
3. Android Content Providers
• Content Providers acts as a medium to various application to
communicate with each other.
• It manages and looks over the application data and accesses
the SQLite Database.
• However, it provides only exclusive data to the application
user and does not allow you to access the other application
data until granted.
• All the seeking of data is done through the content provider
itself.
• Whenever you require to access data from other applications,
the content provider seeks permission from the user to access
the data to the other application.
Examples
• Whenever any application needs to access some
pictures from the gallery, it seeks your permission,
and the content provider provides you with the
required content.
• The payments application which accesses your
contacts to show who is there in your contacts to
exchange money
public class contentProviderName extends ContentProvider {
public void onCreate()
{
// code
}
}
4. Android Broadcast Receivers
• Broadcast Receivers’ task is to receive any kind of
intents from other applications.
• They respond to such intents with some behavior.
Usually, broadcast receivers help to notify users of any
process or application.
• They can notify the user either through notification,
status bar, or even the user’s application(if active).
• It’s not always necessary for your applications to be
active to receive some notifications.
• Most of the time, these broadcasts are system
originated and didn’t require the app’s user interface.
Example
1. Alarm app: Whenever you keep an alarm,
you leave that application or simple terms,
close that application. Still, at a particular
time, you receive the notification.
2. Battery reminder: Whenever your device has
insufficient charge left, your device notifies
you to keep charge or on battery saver mode.
These all happen because of the presence of broadcast
receivers. It helps the system and the user’s applications
to communicate.
Implementation
• For implementing Broadcast Receivers in android,
you need to create a sub-class from the
“BroadcastReceiver” class.
• Each broadcast is made through an “Intent” object.
class MyReceiver : BroadcastReceiver()
{
override fun onReceive(context: Context, intent:
Intent)
{
//Your Code
}
}
Additional Components of Android App
Development
Intents
Widgets
Views
Notifications
Fragments
XML File
Resources
APK Build
1. Intents
• Intents are a powerful component in any android application.
• It allows various applications and various activities to
communicate with each other.
• It carries information of which activity shall be going next or
which activity to stop or destroy.
• We even use intent to transfer data from one activity to
another.
• Intent objects are also used to send or receive broadcasts
generated by the system.
For example:
Whenever you click on the share button on your device,
then the device prompts you with a list of applications
through which you want to share the file.
2. Widgets
• Widgets are a designing and customization
component.
• Whenever you see your device’s home screen,
you can find several widgets like clock,
weather, etc.
• These widgets allow you to change the
appearance of your home screen and even
allow easy access to some often required stuff.
Types of Widgets
Type Functions Example
Control Widget Users can directly access the apps Music player widget might
from phone screen or notification include buttons for play,
areas pause, and skip
Provide shortcuts to commonly
used functions and enabling actions
Information Ddisplay real-time data and current time, date, weather
Widget updates, helping users stay informed conditions, or calendar
about essential details at a glance events
Collection Widget Present information from multiple Widget might display icons
sources or applications to give for recently used apps like
overview of recent activities and phone and WhatsApp,
frequently used apps allowing users to quickly
access these applications
Hybrid Widget Combine features from various A hybrid widget containing
widget types into a single weather forecasts, recent
notifications, news
headlines, and control music
3. Views
• Views are the components that are often used
for designing and event handling purposes.
• Thus, views are part of both designing and
coding. Using the views, we can describe how
an element will behave or interact with the
user.
The most common views in android
• EditText – used to take user inputs.
• ImageView – used to display images.
• Button – decide which event to perform by pressing the button.
• CheckBox – selecting multiple options.
• ImageButton – same as a button but has an image along with it.
• Recycler View – It’s like a list displaying multiple data in the desired layout.
• Nested Scroll View – allows the user to scroll horizontally or vertically.
• Radio Button – allows the user to select only one option from the available.
• Text View – displays text on your layout.
4. Notifications
• It alerts the user of any update or any new announcement by
the application providers.
• Notification can come on both foreground or background of
the app.
• So even if the app is inactive, you still would receive a
notification.
For example:
• Whenever someone messages you on your messaging
application, then immediately you receive a notification.
• Another example can be Flipkart sending notifications to its
user, inviting them to the new upcoming sale.
5. Fragments
• Fragments allow you to provide multiple
functionalities by creating a single activity.
• Using fragments, you can do several tasks on the
same activity itself.
• Fragments are reusable and can be used for various
activities if required.
• The fragment is made of Views and View Pager.
For example:
• In WhatsApp, you can see that there is an activity that
contains chat, status, and call as fragments.
•Using the same activity, you can even chat, keep status or
call someone.
6. XML File
• XML file makes up the layout for any android application.
•Using XML, we can create various designs or layouts for our application.
•XML even allows us to transfer data from the database to our layout file.
7. Resources
•Resources store several things in Android.
• Whether you need a particular font style, image, colors, animations, or
strings, all of these are stored in resources.
•Resources also allow you to put vector images in your android
application.
8. APK Build
•After the development is done, you can combine your code, resources,
and files in a one-unit called APK(Android Application Package).
•This package can be installed on your device by the Package Manager, and
then the user can use that application.