Android Application Components
Applications are made up of a number of components which are initiated when the application starts. Each of these components have their own role in the Android ecosystem and APIs.
Four important Android Application Components are
- Broadcast Receivers
- Content Providers
It is designed to display. It is implemented as the Java Class. Its the main class the users see when they run the application. Activity class are designed to provide GUI to the user. This enables the user to give and take information from an application. An Activity class should implement a single, focused task the user can do.
The Services run in the background. Services have two purposes
- To perform long running operation away from the main UI Thread.
- It provides a way for different processes to request operation and share data.
They listen to and respond to receivers. They play the role the role of subscriber in the subscriber/publisher pattern. Events are represented by INTENT class.
Broadcast receivers receive and respond to the broadcast receivers. An e.g., of the broadcast receivers is the way an SMS is notified. The SMS application software sits waiting for SMS and it broadcasts once it receives an SMS. Then there is another application which corresponds to this intent and the notification is placed on the notification bar.
They allow you to store and share data across different applications. It uses database style interface. It handles low level inter-process communications so that the different applications can share data.
Building an Application
First you create source code and non source code resources that make up our application. The tool compiles the source code and prepares the resources. The output of this step is an Android package or an apk, which corresponds to an exe.
Steps in Creating an Android Application
- Define Resources
- Implement Application Classes
- Package Application
- Install and Run the Application
Android applications are more than source code. They include non source code entities like layouts, strings, Images, Menus and Animations.
Managing the resources separately from the application has many advantages. It allows the Applications to be customized for different devices and users. You can just change the resources and the same gets reflected on the application without changing the source code of the application.
One common resource type is string. There are three types of strings in android
- string array
Plurals are basically string arrays that can be used to chose specific strings that are associated with certain quantities. Such as one book, two books.
A string is typically stored in XML file RES/Values/*.xml. For e.g., <string name = “hello”> Hello World </string>. A string can be accessed by other resources as @string/string_name. In Java a string is accessed as R.string.string_name.
User Interface Layout
The user interface layout is specified in XML files. Some tools allow visual layouts. UI related XML files are usually stored in RES/layout/*.xml.
The same can be accessed in Java using R.layout.layout_name. Other resources can access these files as @layout/layout_name.
Android allows you to create multiple layout files and it allows you to chose those layout files in runtime.
Resources can be accessed using R.Java.something. At compilation time, resources are used to generate the R.Java class.
Implementing Java Classes
This involves implementing atleast one Java class activity. An entry point to Java class activity iis one oncreate() method. The application typically gets initialized by the oncreate() method.
This information is written in Android Manifest.xml. This file contains wide variety of packaging information. They include the application name, list of components that make up the application, permission that need to run the application, the hardware features like camera, the minimum API level on which this application can run.
Install and Run the application
From the Android Studio you can run in the emulator or in the device.
The Activity Class
Activity classes are the primary classes for interacting with the users. It provides a visual interface with which the user can interact with the application. An activity should be a modular. It should focus on one single task such as viewing an email message, showing a login screen. You’ll create an application by sewing together multiple activities together. Each of these activities have single purpose and the user needs to navigate through these different activities.
Navigation through Activities
Android helps in navigating through these activities in several ways. It is done through
- The Task Backstack
- Ensure that the activities are properly suspended, resumed and destroyed as necessary
In Android a task is a set of related activities. These related activities don’t have to be a part of the same application. Most of the tasks start at the Home screen.
When an activity is launched it goes to the top of the backstack as part of the current backstack.
When that activity is destroyed when the user clicks on back button or when the application terminates itself the activity is popped off the backstack.
Backstack is nothing but a stack for different activities based on their priorities. It acts like a stack to store different activities.
The Activity Life Cycle
The Android application have a life cycle. An Activity is created, suspended, resumed and destroyed as necessary when an application executes. An application may not be in total control over the life cycle of these activities.
Some of these actions depend on the choice the user makes. Some of these depend on the android itself. If for e.g., that the device is running low on battery, the Android OS may kill some of the activities to get back the resources.
An activity can be paused
It may be visible but the user will not be interacting with this activity. He may be interacting with another activity. An paused activity can be terminated by the Android. When an activity is stopped it is no longer visible and can be terminated.
Android announces Activity life cycle changes to the Activity by calling specific activity methods like protected void onCreate(Bundle SavedInstanceState), protected void onStart(), protected void onResume(), protected void onRestart(), protected void onStop(), protected void onDestroy().
The Android application doesn’t work by themselves. There is an back and forth collaboration between the Android application and the Android OS itself. You have to understand the rule of these collaborations if you have to have the applications working properly.
When the activities are about to become visible then the Android calls onStart() method or sometimes the onRestart() method.
When the activity is about to become non visible then the android calls the onStop() method.
Visible lifetime of an activity is between onStart() method and onStart() method.
When an activity is visible on the screen, there are times when the user can interact with it and sometimes may not. This happens when the device goes to sleep. In that case the user cannot interact with the activity, even though the activity is in the foreground.
When the activity is again ready for the user interaction then the Android calls the onResume() method.
If the activity is able to stop interacting with the user then the Android calls the onPause() method.
If the activity is able to stop interacting with the user then the Android calls the onPause() method.
If the activity is invisible and in foreground then its life cycle is between onResume() and onPause().
This method is called when Activity is first created. It’ll call super.onCreate() which allows Android to do its own initialization. It’ll set the Activity’s content view. It tells what the Activity’s user interface should be. It retains and capture any information in reference to UI views as necessary. It configures the GUI views as necessary.
This method is called when the activity becomes visible and is about to start to interact with the user. This starts only foreground behaviors like starting animation.
This method is called when we want to switch to another activity. For e.g., when we want to shutdown foreground only behavior.
This method is called when the application is no longer visible to the user. May be this activity can be restarted later.
This method is called when the application needs to be terminated. It helps in releasing the activity resources.
Lets see how one activity can start or call another activity programmatically. First, you need to create an Intent object that specifies the activity that you need to start. Then you need to pass this newly created Intent to methods such as startActivity(), startActivityForResult().
This method will start the desired activity, pushing the current activity away from the foreground.
This method will also act as startActivity() along with the desire that the started activity will return back to the called activity. This is done by calling a callback method, when the called activity finishes to return a result.
Once the new activity is called, then it should return the result back to the calling activity. This can be done by calling the Activity.setResult().
public final void setResult(int resultCode);
public final void setResult(int resultCode, Intent Data);
These resultCode includes some built in data like RESULT_CANCELED, RESULT_OK, CUSTOMRESULTCODE can also be added by the user.
It relates to application resources like languages, keyboard availability, service orientation. Device configuration can change at runtime. On configuration changes the Android kills the current activity and then restarts it.
Since the configuration changes happen regularly/frequently, it needs to be reflected fast. Say for e.g., if the activity orientation is landscape or portrait. The activity can go from landscape mode to portrait mode. This kills the previous activity.
To handle these changes, Java provides two ways to do this. When the changes are occurring, you can create and retain or save the objects containing important state information. Another option is to manually handle the configuration changes.
Retaining an Object
Hard to recompute data can be cached to speed up handling of configuration changes. This is done using Fragment class.
Manually handling the Reconfiguration Changes
You need to declare those specific changes that your activity will handle in AndroidManifest.xml file. For e.g., <activity android:name = “.MyActivity” android:configChanges = “orientation/screensize/keyboardhidden”>. Here the activity name is MyActivity. This will handle the changes to orientation or screen size or keyboard accessibility.
When these configuration changes occur at runtime, the activity’s onConfigurationChanged() method is called. A configuration object is passed to this method specifying the new device configuration. Then our code can read this object and make what ever changes needs to be done.