Frion3L
Frion3L

Reputation: 1522

Pausing or exiting an android game

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!!

Upvotes: 0

Views: 615

Answers (1)

Squagem
Squagem

Reputation: 734

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() and onResume() 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:

protected void onPause(){

   //Save the game state
   //Pause the game loop
   //Save any other data that need to be persistent throughout sessions
}

protected void onResume(){

   //Reinitialize game state
   //Reinitialize any other persistent data
   //Resume the game loop
}

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.

Upvotes: 1

Related Questions