When you are finished with an Activity you can destroy it. This means that it won't be running in the background. You can then call a new instance of the activity. This avoids creating and running several instances at the same time. I have created some basic code below and annotated it.
Menu
public class MenuActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {...}
//Method called when button is clicked
public void PlayAgain(View view){
/*
Start activity used to create the new activity
Calling a new Intent to change from this activity to the GameActivity
*/
startActivity(new Intent(this, GameActivity.class));
//Telling this to finish - ending the activity
this.finish();
}
}
GameActivity
public class GameActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {...}
/*
Method called when criteria is met to end the activity
eg: time limit or score condition
*/
public void onGameFinish(){
//Same as the menu button, changing activities.
startActivity(new Intent(this, MenuActivity.class));
//Ending this activity when we are done with it.
this.finish();
}
}
Hope this helps - All the best.