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();
}
There is no guarantee that finish() will close your current activity immediately(It is upto Android.)
To solve your problem try this,
Hold an reference to the timer (You will be using this to cancel the timer when User leaves the current Activity),
CountDownTimer resultTimer = 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();
and in onBackPressed() - cancel the Timer in setPositiveButton Callback,
@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) {
resultTimer.cancel(); //Stop the timer
resultTimer = null;
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();
}
The property of CountDownTimer
is , even you finished the activity it continues to countDown. If you want to stop this, put countTimerVar.cancel()
when you finish()
and in pressBackButton
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With