Thursday, February 15, 2018

Android Application Components

Before you start developing your own Android applications, it is important to understand how
Android applications are constructed and to have an understanding of the Android application life cycle.
In this chapter you will learn about Android application components that make up an Android application. You will also learn about Android application life cycle and managing resources.
Android Application Components
Android application components are building blocks of an Android application. Each component is a different point through which the system can enter your app and they also interact each other.
There are four major types of application components. Each type of component has  a distinct purpose and has a distinct lifecycle that defines how the component is created and destroyed.
Activity
 An activity represents a single screen with a user interface with which user can interact. For example when you open the phone/dialer application you see dialer screen, this is an activity. Another example when you open messaging application you see a list of SMS this is an activity. When you click on compose button a new screen for composing SMS gets open this is another Activity.
An activity is implemented as a subclass of Activity class, you need to extend Activity class and override some of the methods as per your requirement. Activity and it’s life cycle will be described in detail later in this chapter.
Services
 A service is a component that runs in the background to perform long-running operations. A service does not provide a user interface. For example, a service can vibrate device in the background, a service can download an application in the  background or a service can transfer file over Bluetooth in background.
A service is implemented as a subclass of Service class, you need to extend Service class and override some methods as per your requirement. Service and its life cycle will be described in detail later in this chapter.
Broadcast receivers
A broadcast receiver is a component that responds to system generated broadcast announcements. Android system  sends announcements  when some events occur.  All registered receivers for an event are notified by the Android runtime once this event happens. For example android system broadcast announcements when a SMS is received, when there is an incoming call, when boot process completes, when battery is low etc.
To receive these broadcasts we need to register our application for respective broadcast for example if we want to receive broadcast when a SMS received we need to register the application for SMS_RECEIVED event.
Broadcast receivers are implemented using BroadcastReceiver class, you need to extend BroadcastReceiver class and override onReceive() method. Broadcast receivers will be described in detail later in this chapter.
Content provider
A content provider allows applications to access data. Content provider component supplies data from one application to others on request. A SQLite database is private to the application which creates it. If you want to share your application data with other applications you need to use a content provider.
We also use content provider to access other application’s data. For example you can access the phonebook contacts using content providers.
Note: All the Android components must be added/declared in manifest file with respective under application tag. How to add these components in manifest file is described below in respective sections.
Let us understand all these Android components in detail.
Activities
An Activity is an application component that provides a screen containing user interface with which users can interact in order to do something. An application can have one or  multiple activities that are loosely bound to each other. Typically, one activity in an application is specified as the "main" activity, which is presented to the user when launching the application is launched for the first time. Each activity can then start another activity in order to perform different actions.
Declaring the activity in manifest file
An activity is declared in manifest by activity tag.
<activity android:label="labelName"
      android:name=".MyActivity">
</activity>
You can also add action and category attributes.
The <action> element specifies that this is the "main" entry point to the application. The <category> element specifies that this activity should be listed in the system's application launcher (to allow users to launch this activity).
To make an activity launcher activity that will be execute when application will be launch, you need to mention it in manifest file like below
<activity android:name=".ExampleActivity">
   
<intent-filter>
       
<action android:name="android.intent.action.MAIN" />
       
<category android:name="android.intent.category.LAUNCHER" />
   
</intent-filter>
</activity>

Note: All your application activities must be declared with <Activity> tag in manifest, if you do not declare them in manifest you will get exception “ActivityNotFoundException”.

Creating an Activity
To create an activity, you need to extend  Activity class. In your subclass, you need to implement callback methods which will be call by android system when the state of the activity gets changed such as when the activity is being created, stopped, resumed, or destroyed. The two most important callback methods are:
You must implement this method. This method is called by system when creating your activity. In this method you need to set your user interface with which user will interact. Most importantly, this is the method where you must call setContentView() to define the layout for the activity's user interface.
The system calls this method when another activity gets into the foreground and current activity goes in background. When an activity starts another activity, the current activity goes in pause state.

  There are several other lifecycle callback methods that you can override in order to provide a effective user experience between activities.

1 comment: