2

I try to create LUDO game and when user stop the game I want to save game state (Activity state).

I understood that I need to save state in method OnPause() but there are lots variables that I need to save and there are all changing in time so I am not sure that I can save state in that way.

So can I save the whole Activity and OnResume() method to restored it and just continue whith the game?

Thanks.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
Kec
  • 1,435
  • 4
  • 16
  • 21

2 Answers2

1

I think you are looking for How to save an Android application's state.

The documentation also provides details on saving persistent state with onPause.

Community
  • 1
  • 1
Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • This is how game should work. It refresh screen constantly and when user etc. click on player to eat other play I create animation (changing parameters in time), so it's possible to change some parameters and then user can exit from app and some parameters will not be synchronized(if user exit in the middle of some method). So I need to save whole Activity if it is possible and when I restored it, that I continue where Activity stopped (from middle of method) last time. Is that possible? Do you have same suggestion? Thanks. – Kec May 26 '12 at 08:57
  • 1
    When an activity exits, it will *always* call `onPause`, so you probably want to save a lot of the state in that method. – Alex Lockwood May 26 '12 at 17:37
1

Don't know what you mean by changing in time. Are you saying your game progresses even while the player isn't playing? Normally, you would save only the minimum amount of information necessary to recreate the current state. For a board game, this could be the locations of each players' pieces, and whose turn it is.

If you really have so much game state that it would be unwieldy to save and restore, or the game has to progress while the Activity is closed, or recreating the state from static data is too difficult (all propositions I would probably dispute, but we can take it a given for the sake of discussion), then I would suggest you write it as a client-server app, where an Android Service runs the game engine and your Activity does all the visuals and user interaction.

Sparky
  • 8,437
  • 1
  • 29
  • 41
  • This is how game should work. It refresh screen constantly and when user etc. click on player to eat other play I create animation (changing parameters in time), so it's possible to change some parameters and then user can exit from app and some parameters will not be synchronized(if user exit in the middle of some method). So I need to save whole Activity if it is possible and when I restored it, that I continue where Activity stopped (from middle of method) last time. Is that possible? – Kec May 25 '12 at 22:42
  • It would not be practical to freeze the whole Activity and restore it precisely. You should figure out which local variables are important to recreate a suitable facsimile of the original. (See http://stackoverflow.com/a/151940/517561 for a code sample.) Minor details like exactly which phase of a cyclical animation a mob is in are probably not worth the effort. – Sparky May 26 '12 at 19:37