Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity still continues after finish();

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();
}
like image 414
omi0301 Avatar asked Oct 23 '22 09:10

omi0301


2 Answers

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();
}
like image 99
AjOnFire Avatar answered Oct 29 '22 17:10

AjOnFire


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.

like image 41
user996428 Avatar answered Oct 29 '22 19:10

user996428