# android game saving game state

I'm developing a game and i've ran into some questions about saving and resuming the game. Let's say i have 10 animated sprites drawn every frame (which move side to side) and everytime i press the home button or back button and then go back to my game, it starts drawing the sprites at the starting position rather than in the position before the back/home press.

• Do i have to save every sprite and animation position in order to achive resuming the game where i left off (using Bundle, onSaveInstanceState and onRestoreInstanceState())? Or can this be achived some other simpler way.

I'm using the lunarlander example which handles the drawing in a thread and when the user presses the home/back button(onPause) the state is saved and then reloaded on resume.But if i move all the drawing methods to SurfaceView (LunarView) and just call them from a thread(LunarView.draw()), the sprites stay in the exact same position as before the back/home button pressed without saving anything. Can someone explain why this happens? Is it something i should avoid(calling SurfaceView.draw() from a thread) or is this the anwser to my question how to save game state?

Regards, J.

• is your initialization function called when you resume game? Jun 6 '11 at 14:44
• ups sorry i see now why this is happening.. when resume was called the initialization was called also.. stupid me :S I see now that the LunarLander example saves it's state only to save the users state even if u kill the game's process not only if u press the home button. Jun 6 '11 at 15:03
• exactly my point. Jun 6 '11 at 15:08

If I have to guess I think you are calling initialize function every time you try resuming the game. You just have to avoid calling it when the game is resumed. Remember there is a diffrence between launching game and resuming it, and initilize is only used for launching game.

• how can you check if this is happening? is it the views constructor or its onCreate() you dont want to call on resume? or maybe both? cus I have this problem with the classes that get their x and yvalue set in their constructor. Sep 29 '12 at 14:30
• @max as far as I know different events are called for each of resuming and initializing, but I don't know what are those events. Sep 29 '12 at 16:38