I have a quiz app which has a timer for the whole gameactivity where in you should answer as much questions you can within the assigned minutes..
after the assigned minutes is over, it will take you to the results activity which shows your score. On back press,I created an alert dialog that asks if you want to go back to the main menu. If clicked on yes, the page should go back to the main menu and stop/kill the gameactivity.
However, when I click on "yes", it will go back to the main menu but while you are still anywhere in the application, the results would still show from the previous gameactivity i had. maybe i hadnt really finished the gameactivity, .. Here is the timer and back press snippet 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();
}