I’m trying to do something that ought to be pretty simple.
I have a game which launches a StartActivity with a screen offering various options, one of which is “start game”.
When the user chooses this option, the game proper is started with code akin to:
Intent i = new Intent();
i.setClass(context, GameActivity.class);
context.startActivity(i);
This all works fine. When in the game the “back” button is pressed, control returns to this StartActivity. Again, this is fine.
However, pressing “back” causes “onDestroy()” to be called (as per the docs) – this means that when “start game” is pressed again, the GameActivity starts from scratch.
What I would like is for the GameActivity to be kept in memory when “back” is pressed, so that pressing “start game” returns to where it had left off.
This is something nearly every game does, so must be pretty straightforward really, but I can’t work out how to do it. I’ve been trying to overload onBackPressed() in such a way as to return to the StartActivity, but without success; doing something like:
public void onBackPressed() {
Intent i = new Intent(this, StartActivity.class);
startActivity(i);
}
means the stack grows eternally, ie A -> B -> A leaves ABA rather than just A.
I’m aware of the general activity flow (http://developer.android.com/reference/android/app/Activity.html).
Am I launching the GameActivity in the wrong way?
#
In response to wingman, I am aware of the various activity states, and that onDestroy() is likely to be called if required to free memory. I do save game state on onPause() and load on onResume(), but not the complete state the memory is in at that time.
If I press “home” the app is “minimised” in effect, and returns to where it was when relaunched – onDestroy() is not called. I’m trying to emulate this behaviour but return the app to the StartActivity instead of “home”.
The back button always calls onDestroy() as I far as I can gather from the docs (or finish() at least).
Do all games save a complete state when this happens? I’d be surprised, but maybe they do.
No Activity is destroyed completely when back button is pressed. Android maintains a stack of running Activities. when user presses back button, the current activity is popped off the stack, and the previous one shows. But the popped off activity is not destroyed, its still in memory. It will be only destroyed when system runs low on memory, and only then,
onDestroy()will be called.Before your activity goes away from screen due to user pressing back, android calls a number of methods of your activity, where you can save its state, and restore it when user again navigates to your activity. Go through the Activity life cycle documentation and samples here : http://developer.android.com/reference/android/app/Activity.html
So, Save your game activity state on one of the
onPause()oronStopped()methods, On the main menu, if user selects “Resume” , Start your game Activity from its saved state, else if user selects “New Game” , Start your game activity from scratch.