Game Development Reference
In-Depth Information
Key Activity Life Cycle Cases
There are some key situations that you have to consider when programming your Activity class.
Another activity comes to the foreground : The current Activity is paused; that is, the
Activity's onPause() function is called.
The Power key is turned off : The current Activity's onPause() function is called; the
Power key is turned back on; and then the Activity's onResume() function is called,
followed by a return to resumption of the activity.
Phone orientation changes : The current Activity's onPause() function is called. The
Activity's onStop() function is called. The Activity's onDestroy() function is called.
Finally, a new instance of the previous Activity is created with the new Orientation
and onCreate() is called.
Back key is pressed : The current Activity's onPause() function is called. The Activity's
onStop() function is called. Finally, the Activity's onDestroy() function is called. The
Activity is no longer active.
Home key is pressed : The current Activity's onPause() function is called. The
onStop() function is called, and the user is taken to the home screen where other
activities can be started. If the user tries to begin the previously stopped Activity
by clicking its icon, the previous Activity's onRestart() function is called. Next, the
onStart() function is called. The onResume() function is then called. The Activity
becomes active again and is running.
The important concept to take away from Figure 2-1 is that you should save the game state
whenever onPause() is called.
Seeing the Activity Life Cycle in Action
Listing 2-1 shows how these callback functions look inside our new MainActivity class that we
created in Chapter 1. The log statements added into each callback output error log messages to the
LogCat window indicating which callback is being executed. Try to type in the extra code and run
the program and see for yourself the life cycle callbacks being executed.
Listing 2-1. “RobsHelloWorld” Example with Life Cycle Callbacks Added
package com.robsexample.robshelloworld;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
private static final String TAG = "MyActivity";
@Override
 
Search WWH ::




Custom Search