I’m developing an Android game with all the typical stuff it envolves: the game loop, renderer, panel, …
The problem is that when I exit the game, it always crashes… I think it’s because I don’t stop correctly the game loop and it continues. Also, I would like to pause it when I “minimize” the game or I switch the mobile phone to stand by.
What should I put in onPause and onDestroy?
I only have this:
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
super.surfaceDestroyed(holder);
//Stop game loop:
loop.setRunning(false);
boolean retry = true;
while (retry)
{
try
{
//gameLoop.join();
loop.stop();
retry = false;
}
catch (Exception e)
{
// try again shutting down the thread
}
}
}
But it is not enough and it is only for exiting.
Thanks!!
In order to ensure that your application is behaving “normally” (normally meaning that it is behaving the way you want it to), you should override the
onPause()andonResume()method in the activity that contains your game loop. Given that you haven’t really provided much other code, it makes sense that this is something you simply forgot to implement.For instance, here is some abstract psuedo-code to accomplish your goal:
This is understandably something very easily overlooked if you have never developed an Android application before.
For additional explanation on Android activity states, see this image, taken from the Android Activity documentation.