Thursday, July 25, 2013

Contd2: How to build an Android application, step by step


Managing activity state


Applications can be interrupted when various higher-priority events, such as phone calls, take precedence. There can be only one active application at a time; specifically, a single application activity can be in the foreground at any given time.
Android applications are responsible for managing their state, as well as their memory, resources and data. The Android operating system may terminate an activity that has been paused, stopped or destroyed when memory is low. This means that any activity that is not in the foreground is subject to shutdown. In other words, an Android application must keep state and be ready to be interrupted and even shut down at any time.

Using activity callbacks

The Activity class has a number of callbacks that provide an opportunity for an activity to respond to events such as suspending and resuming. The table below lists the most important callback methods.

Key callback methods of Android Activities

Callback MethodDescriptionRecommendations
onCreate()Called when an activity starts or restarts.Initializes static activity data. Binds to data or resources required.
Sets layout with setContentView().
onResume()Called when an activity becomes the foreground activity.Acquires exclusive resources. Starts any audio, video, or animations.
onPause()Called when an activity leaves the foreground.Saves uncommitted data.Deactivates or releases exclusive resources.
Stops any audio, video, or animations.
onDestroy()Called when an application is shutting down.Cleans up any static activity data. Releases any resources acquired.
The main thread is often called the UI thread, because this is where the processing for drawing the UI takes place internally. An activity must perform any processing that takes place during a callback reasonably quickly, so that the main thread is not blocked. If the main UI thread is blocked for too long, the Android system will shut down the activity due to a lack of response. This is especially important to respond quickly during the onPause() callback, when a higher-priority task (for example, an incoming phone call) is entering the foreground.
The image below shows the order in which activity callbacks are called.
Callback methods of the activity life cycle
Important callback methods of the activity life cycle.

Saving activity state

An activity can have private preferences - much like shared application preferences. You can access these preferences by using the getPreferences() method of the activity. This mechanism is useful for saving state information. For example, PlayActivity for your game might use these preferences to keep track of the current level and score, player health statistics and game state.

Shutting Down Activities

To shut down an activity, you make a call to the finish() method. There are several different versions of this method to use, depending whether the activity is shutting itself down or shutting down another activity.
Within your game application, you might return from the Scores, Play and Help screens to the Menu screen by finishing ScoresActivity, PlayActivity or HelpActivity.

Working with Intents

An Intent object encapsulates a task request used by the Android operating system. When the startActivity() method is called with the Intent parameter, the Android system matches the Intent action with appropriate activity on the Android system. That activity is then launched.
The Android system handles all intent resolution. An intent can be very specific, including a request for a specific activity to be launched, or somewhat vague, requesting that any activity matching certain criteria be launched. For the finer details on intent resolution, see the Android documentation.

Passing information with intents

Intents can be used to pass data between activities. You can use an intent in this way by including additional data, called extras, within the intent.
To package extra pieces of data along with an intent, you use the putExtra() method with the appropriate type of object you want to include. The Android programming convention for intent extras is to name each one with the package prefix (for example,com.androidbook.chippy.NameOfExtra).
For example, the following intent includes an extra piece of information, the current game level, which is an integer:
Intent intent = new Intent(getApplicationContext(), HelpActivity.class);

intent.putExtra("com.androidbook.chippy.LEVEL", 23);
startActivity(intent);
When the HelpActivity class launches, the getIntent() method can be used to retrieve the intent. Then the extra information can be extracted using the appropriate methods. Here's an example:
Intent callingIntent = getIntent();

int helpLevel = callingIntent.getIntExtra("com.androidbook.chippy.LEVEL", 1);
This little piece of information could be used to give special Help hints, based on the level.

No comments:

Post a Comment

Kindly share your view or contribution on this topic