Activity continues after the finish ();

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();
}
+5
source share
3 answers

There is no guarantee that finish () will immediately close your current activity (this is before Android.)


To solve your problem, try this,


Keep a link to the timer (you will use this to cancel the timer when the 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 the 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();
}
+2
source

CountDownTimer - , , . , countTimerVar.cancel(), finish() pressBackButton.

+1

startActivityForResult onActivityResult . : ParentActivity ChildActivity startActivityForResult(). , ParentActivity. finish() ChildActivity. ParentActivity ChildActivity, ParentActivity (). .

For code, see http://www.androidcompetencycenter.com/2009/03/tutorial-how-to-start-a-new-activity/

0
source

All Articles