I have a quiz app that has a timer for all game activity, where you have to answer as many questions as you can within the assigned minutes.
after the appointed minutes have ended, it will transfer you to the results that show your result. When I clicked back, I created a warning dialog box that asks if you want to return to the main menu. If you click "Yes", the page should return to the main menu and stop / kill the game.
However, when I click "yes", it will return to the main menu, but while you are still in the application, the results will still be displayed from the previous game activity. maybe I really didn’t finish the game ... Here is a timer and back excerpt from my activity:
new CountDownTimer(seconds, 1000) {
public void onTick(long millisUntilFinished) {
timer.setText("Seconds left: " + millisUntilFinished / 1000);
}
public void onFinish() {
Intent intent = new Intent(GameActivityAddition.this, Score.class);
intent.putExtra("totalscore", score);
intent.putExtra("numberquestions", rowId);
intent.putExtra("d", difficulty);
db.close();
startActivity(intent);
}
}.start();
@Override
public void onBackPressed() {
AlertDialog.Builder abuilder = new Builder(GameActivityAddition.this);
abuilder.setMessage("Return to Main Menu?");
abuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent main = new Intent(GameActivityAddition.this, Main.class);
startActivity(main);
finish();
}
});
abuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = abuilder.create();
alert.show();
}
source
share