Game Development Reference
In-Depth Information
Getting ready
If you are not familiar with Android Activity lifecycle, refer to the developer manual: http://
developer.android.com/training/basics/activity-lifecycle/index.html .
How to do it…
1.
An Android application does not have to implement all of the lifecycle methods. Our
strategy for lifecycle management will be very simple; save game state and terminate
an application once the onPause() method is called. We need to write some
Java code to make it work. Add this code to your Activity class, in our case it is
Game1Activity in the Game1Activity.java ile:
@Override protected void onPause()
{
super.onPause();
ExitNative();
}
public static native void ExitNative();
2.
Implement the ExitNative() JNI method in the following way:
JNIEXPORT void JNICALL Java_com_packtpub_ndkcookbook_game1_
Game1Activity_ExitNative(
JNIEnv* env, jobject obj )
{
OnStop();
exit( 0 );
}
3.
Now we can implement the native OnStop() callback in our game.
How it works…
A typical implementation of the OnStop() callback will save the game state, so it can be
restored when the game resumes later. Since our irst game does not require any saving, we
will provide only an empty implementation:
void OnStop()
{
}
You may want to implement game saving later as an exercise.
 
Search WWH ::




Custom Search