Monitoring State Changes

To ensure that Activities can react to state changes, Android provides a series of event handlers that are fired when an Activity transitions through its full, visible, and active lifetimes. Figure 3-7 summarizes these lifetimes in terms of the Activity states described in the previous section.

Activity is Killable

Activity is Killable

FIGURE 3-7

FIGURE 3-7

The skeleton code in Listing 3-12 shows the stubs for the state change method handlers available in an Activity. Comments within each stub describe the actions you should consider taking on each state change event.

LISTING 3-12: Activity state event handlers Available for downloadon package com.paad.myapplication; Wrox.com import android.app.Activity; import android.os.Bundle;

public class MyActivity extends Activity {

// Called at the start of the full lifetime. ©Override public void onCreate(Bundle savedlnstanceState) { super.onCreate(savedlnstanceState); // Initialize activity.

// Called after onCreate has finished, use to restore UI state ©Override public void onRestoreInstanceState(Bundle savedlnstanceState) { super.onRestorelnstanceState(savedlnstanceState); // Restore UI state from the savedlnstanceState. // This bundle has also been passed to onCreate.

// Called before subsequent visible lifetimes

// for an activity process.

©Override public void onRestart(){ super.onRestart();

// Load changes knowing that the activity has already // been visible within this process.

// Called at the start of the visible lifetime. ©Override public void onStart(){ super.onStart();

// Apply any required UI change now that the Activity is visible.

// Called at the start of the active lifetime. ©Override public void onResume(){ super.onResume();

// Resume any paused UI updates, threads, or processes required // by the activity but suspended when it was inactive.

// Called to save UI state changes at the // end of the active lifecycle. ©Override public void onSaveInstanceState(Bundle savedlnstanceState) { // Save UI state changes to the savedlnstanceState. // This bundle will be passed to onCreate if the process is // killed and restarted.

super.onSavelnstanceState(savedlnstanceState);

// Called at the end of the active lifetime. ©Override continues

LISTING 3-12 (continued)

public void onPause(){

// Suspend UI updates, threads, or CPU intensive processes // that don't need to be updated when the Activity isn't // the active foreground activity. super.onPause();

// Called at the end of the visible lifetime. ©Override public void onStop(){

// Suspend remaining UI updates, threads, or processing // that aren't required when the Activity isn't visible. // Persist all edits or state changes

// as after this call the process is likely to be killed. super.onStop();

// Called at the end of the full lifetime. ©Override public void onDestroy(){

// Clean up any resources including ending threads, // closing database connections etc. super.onDestroy();

As shown in the preceding code, you should always call back to the superclass when overriding these event handlers.

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Post a comment