Making a restart button for an Android app game

I am working on an android app and when I run the main activity the app runs as I want it to but stops after you die in the game. I want to make a restart button appear after you die in the game. So far I've made a new layout with a button and the background of the main activity. I made a new class in my source folder and have set up an onclicklistener but I'm not sure what code will make the the button start the main activity start again.

Answers


you can add the following code to your onclick method:

finish();
startActivity(getIntent());

by adding the following code, you are restarting the same activity... without going back to the previous one.

UPDATE

here how you can change and restart the game, you need to add flags to start the activity over.

    Intent i = new Intent(this, MainMenu.class); //change it to your main class
    //the following 2 tags are for clearing the backStack and start fresh
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    finish();
    startActivity(i);

try it out, hope it works for you


You could just call Activity.recreate(); if you're api level is 11 or above.


Need Your Help

matplotlib animation produces a blank

python animation matplotlib

I have a bunch of PNGs that I want to turn into an animation. I use matplotlib for this. I can display the resulting animation on the screen and everything looks fine. But the saved MP4 is just a