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.

0

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
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 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.
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.

2 Comments

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.
When an activity exits, it will always call onPause, so you probably want to save a lot of the state in that method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.