Petar Minchev
Petar Minchev

Reputation: 47373

Save game state in OnPause or OnSaveInstanceState

I am a newbie to Android, and I am developing a simple maze game for educational purposes. I am confused how should I react to the OnSaveInstanceState and OnPause callbacks. Basically I want to save the state of the game, so when there is an orientation change or the user presses back button, the game should be resumed from the saved state.

I have read that there is no guarantee that OnSaveInstanceState will be called. Then why should I use it all? I am confused.

Which callback should I use for saving the game state? Should I use only OnPause and save the game state to a file in the internal storage of the device?

How would you tackle this situation?

Upvotes: 1

Views: 323

Answers (1)

Blackbelt
Blackbelt

Reputation: 157437

The difference between onPause() and onSaveInstanceState() is that the latter is not part of the Activity's lifecycle. While you do have the guarantee that onPause() is called in every situation, that's not true for onSaveInstanceState(). As per documentation persistent data should be saved on onPause(). For instance during normal navigation between Activityies, the pair onSaveInstanceState/onRestoreInstanceState is not called

Upvotes: 2

Related Questions