I now have a MainMenu activity, with a XML file for it’s layout. By pressing a button an Intent brings the user to the activity of the game, which has a GLSurfaceView.
I use my ‘Game’ class to manage all game objects, lives, and resets etc… When lives reaches 0 I want to call onGameOver(), which should return you to the MainMenu activity. How do I accomplish this?
I tried having a static call to my Activity Class, to either call:
moveTaskToBack(true);
startActivity(intentToMainMenu);
I have just no Idea how I can return to the mainmenu. Also I believe I should destroy the activity of the game, since the OpenGL context will be lost if I switch activities. So how do I do this?
You could do like Frank posted, but in your particular case you probably don’t want to finish of the game activity(the player will probably play again). No context will be lost. If you finish of you game activity, starting a new one every time will be slow. Because of this, it’s better to start activities like this:
The reorder to front flag makes sure you don’t have multiple instances of the same activity because it doesn’t start a new activity if one already exists. So when the player runs out of lives you simple start MainMenu like the example above.
PS: You can’t finish off activities statically.
In light of your comment, here’s an update:
Although there probably are better solutions, here’s a solution that I use: Since you are creating a game you probably have a separate thread that draws stuff and checks whether the game has finished. Register a handler for the rendering thread that exists in the UI thread in your Activity. Then you will be able to post a message to the Handler in the Activity class when the game should end. On the receiving end, you just do the code posted in my answer since you won’t be in a static method.
If you are unfamiliar with handlers: http://saigeethamn.blogspot.com/2010/04/threads-and-handlers-android-developer.html