Android Application Components
Android Application Components
Application components are the essential building blocks of an Android application. These
components are loosely coupled by the application manifest file AndroidManifest.xml that
describes each component of the application and how they interact. There are following four
main components that can be used within an Android application –
Activities
They dictate the UI and handle the user interaction to the smart phone screen.
Services
They handle background processing associated with an application.
Broadcast Receivers
They handle communication between Android OS and applications.
Content Providers
They handle data and database management issues.
ACTIVITY
If you have worked with C++ or Java programming language then you must have seen that your
program starts from main() function. Very similar way, Android system initiates its program
with in an Activity starting with a call on onCreate() callback method. There is a sequence of
callback methods that start up an activity and a sequence of callback methods that tear down an
activity as shown in the below in the activity life cycle diagram
The Activity class defines the following call backs i.e. events. You don't need to implement all
the callbacks methods. However, it's important that you understand each one and implement
those that ensure your app behaves the way users expect.
onCreate()
This is the first callback and called when the activity is first created.
onStart()
This callback is called when the activity becomes visible to the user.
onResume()
This is called when the user starts interacting with the application.
onPause()
The paused activity does not receive user input and cannot execute any code and called when
the current activity is being paused and the previous activity is being resumed.
onStop()
This callback is called when the activity is no longer visible.
onDestroy()
This callback is called before the activity is destroyed by the system.
onRestart()
This callback is called when the activity restarts after stopping it.
Broadcast Receivers
They simply respond to broadcast messages from other applications or from the system itself.
These messages are sometime called events or intents. For example, applications can also
initiate broadcasts to let other applications know that some data has been downloaded to the
device and is available for them to use, so this is broadcast receiver who will intercept this
communication and will initiate appropriate action.
There are following two important steps to make BroadcastReceiver works for the system
broadcasted intents −
There is one additional steps in case you are going to implement your custom intents then you
will have to create and broadcast those intents.
Content providers let you centralize content in one place and have many different applications
access it as needed. A content provider behaves very much like a database where you can
query it, edit its content, as well as add or delete content using insert(), update(), delete(), and
query() methods. In most cases this data is stored in an SQlite database.
Content URIs
To query a content provider, you specify the query string in the form of a URI which has
following format –
<prefix>://<authority>/<data_type>/<id>
prefix
This is always set to content://
authority
This specifies the name of the content provider, for example contacts, browser etc. For third-
party content providers, this could be the fully qualified name, such
as com.tutorialspoint.statusprovider
data_type
This indicates the type of data that this particular provider provides. For example, if you are
getting all the contacts from the Contacts content provider, then the data path would
be people and URI would look like thiscontent://contacts/people
Id-This specifies the specific record requested. For example, if you are looking for contact
number 5 in the Contacts content provider then URI would look like
this content://contacts/people/5.