Search code examples
androidandroid-activityback-button

Hitting Back Button and then Resuming Activity


I am developing a game for Android and have what I imagine to be a very common scenario, but I can't figure out how to do it.

I have a Title Screen where the user can select "New Game". When they start up a game, I push a GameActivity onto the stack. Then if the user hits the physical back button the phone, the application goes back to the TitleActivity, as I intend it. However, I need a way to allow the user to resume the game at this point, but I don't know how to get back to htat instance of the GameActivity. Any help is appreciated.


Solution

  • You can't guarantee that you will get back to that instance of GameActivity you need to save the game state somehow, and make your GameActivity able to resume from that saved state.

    Make sure you understand the Activity lifecycle. The instance of your activity can be killed at any time when it isn't visible, so you need to save state at the proper point (onSaveInstanceState).