Life Cycles of the Rich and Famous

During its lifetime, each activity of an Android program can be in one of several states, as shown in Figure 2.3, on the next page. You, the developer, do not have control over what state your program is in. That's all managed by the system. However, you do get notified when the state is about to change through the onXX() method calls.

You override these methods in your Activity class, and Android will call them at the appropriate time:

• onCreate(Bundle): This is called when the activity first starts up. You can use it to perform one-time initialization such as creating

Figure 2.3: Life cycle of an Android activity

the user interface. onCreate( ) takes one parameter that is either null or some state information previously saved by the onSaveIn-stanceState() method.

• onStart(): This indicates the activity is about to be displayed to the user.

• onResume(): This is called when your activity can start interacting with the user. This is a good place to start animations and music.

• onPause(): This runs when the activity is about to go into the background, usually because another activity has been launched in front of it. This is where you should save your program's persistent state, such as a database record being edited.

• onStop(): This is called when your activity is no longer visible to the user and it won't be needed for a while. If memory is tight, onStop( ) may never be called (the system may simply terminate your process).

0 0

Post a comment