ianos
ianos

Reputation: 153

Switch between existing activities in Android

So I am developing this small game, it currently uses 2 activities: The Menu Activity and the actual Game Activity; The Game Activity is started by the Menu Activity (no problems at that)

Then I can switch from the Game Activity to the Menu Activity by using the back Key:

public void onBackPressed() {
    super.onBackPressed();
}

Now I am back in the Menu Activity. The Game Activity I've created has been stopped(I guess); How do I go back to the Game Activity, as I left it(how do I restart it)?

Upvotes: 2

Views: 639

Answers (3)

Matt Robertson
Matt Robertson

Reputation: 3165

Assuming you are using the menu as a sort of "pause" menu where the back button pauses the game, you could have the back button create a new intent for the menu activity and then let the back button from the menu activity function as normal.

Of course, you should still preserve game data when you launch the menu activity - since the user could close the screen from the menu - but the system will inherently keep your game activity (and data) in memory when you launch the menu.

@Override
public void onBackPressed() {
   Intent intent = new Intent(GameActivity.this, MenuActivity.class);
   startActivity(intent);
}

Upvotes: 0

Snicolas
Snicolas

Reputation: 38168

It is absolutely pointless to override a method just to call super. Please re-check what overriding means : if you don't override, then your subclass method A will be the same as super class method A. Thus, you can override to create a different version of A in your subclass, but not to call the super class method A, it is useless.

About the Activity, in android, you can't hope than activity that is not on the stack will be preserved. You can add this to your manifest :

 <activity name=".GameActivity" 
   ... 
   android:launchMode="singleTop" />

but even though, it will not guarantee that android will just clean up your activity and recreate it when it is relaunched. That's the way android preserves resources.

But a better way would be to call (startActivity) MenuActivity again from inside GameActivity. That way, your old GameActivity will still be in the activity stack. But really, there will always be some limit cases where android will recreate GameActivity. For instance, if system is under memory pressure.

The best is to persist the state of GameActivity so that even a new instance will be re-put in the same state as the old instance was.

Upvotes: 1

Sam
Sam

Reputation: 86958

You need to manually save the state of your game using any of the methods described in Data Storage.

However if you start a new copy of the Menu Activity:

Menu Activity ->
    Game Activity ->
        Menu Activity

You can return to the Game Activity rather than completely rebuilding it. But of course, there is no guarantee that game Activity won't be destroyed by the garbage collector, so you still need to save the Game's state in onPause().

Upvotes: 1

Related Questions