0

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

Frion3L
  • 1,502
  • 4
  • 24
  • 34
  • Need code and logcat stack trace. – Tim Nov 05 '12 at 17:36
  • 1
    Just try to imagine that you are me. All I have is the text you have typed. How would you answer the question? – Simon Nov 05 '12 at 17:41
  • I don't have any code for those functions. That's why is crashing. I don't know how to stop the loop correctly. I only have the onSurfaceDestroy. – Frion3L Nov 05 '12 at 17:48

1 Answers1

1

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.

Squagem
  • 724
  • 4
  • 17
  • How could I save the game state? I mean, all the stuff is being destroyed when I minimize the game :l Where can I save it? – Frion3L Nov 05 '12 at 23:49
  • It really depends on your implementation. You can always make data persistent with a simple database, or even just write the values to a file during each iteration of the `onPause()` call. You could then read the respective files in the `onResume()` call. For assistance on pursuing the former option, research SQLite data basing (you may get started [here](http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html)), for the latter research [BufferredWriters](http://docs.oracle.com/javase/1.4.2/docs/api/java/io/BufferedWriter.html) to write information to a file. – Squagem Nov 06 '12 at 06:07