开发者

Alertdialog remaining alive after click

开发者 https://www.devze.com 2023-03-13 05:16 出处:网络
I have a weird bug in my program which I can only explain in one way. When I click on a certain button in my menu screen the user gets an AlertDialog which asks him how he wishes to proceed.

I have a weird bug in my program which I can only explain in one way.

When I click on a certain button in my menu screen the user gets an AlertDialog which asks him how he wishes to proceed. Whichever he chooses he is then passed on to another activity.

Thing is, than when the user exits that activity and thus returns to the menu activity, the dialog seems to be still open. Thing is that it doesn't happen every time.

I haven't put a dismiss() in my code but I was sure that it does so automatically.

            AlertDialog alertDialog = new AlertDialog.Builder(YanivMenuActivity.this).create();
            alertDialog.setTitle("Active Game");
            alertDialog.setMessage("You are in the middle of a game.\nStarting a new game will cancel that one.\nHow do you wish continue?");
            alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Start New Game", 
                      new DialogInterface.OnClickListener(){
                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            inGame = false;
                            startActivity(new Intent(YanivMenu开发者_运维知识库Activity.this,YanivGameActivity.class).putExtra("reset", true));
                        }
                      });

            alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Continue Old Game", 
                      new DialogInterface.OnClickListener(){
                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            startActivity(new Intent(YanivMenuActivity.this,YanivGameActivity.class).putExtra("reset", false));
                        }
                      });
            alertDialog.show();


You've almost answered your own question. You need to call dismiss() just before your startActivity(...); call.


Please check below code

 AlertDialog alertDialog = new AlertDialog.Builder(YanivMenuActivity.this).create();
        alertDialog.setTitle("Active Game");
        alertDialog.setMessage("You are in the middle of a game.\nStarting a new game will cancel that one.\nHow do you wish continue?");
        alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Start New Game", 
                  new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        inGame = false;
                       dismiss();
                        startActivity(new Intent(YanivMenuActivity.this,YanivGameActivity.class).putExtra("reset", true));
                    }
                  });

        alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Continue Old Game", 
                  new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                       dismiss();
                        startActivity(new Intent(YanivMenuActivity.this,YanivGameActivity.class).putExtra("reset", false));
                    }
                  });
        alertDialog.show();


Before calling start activity you have to cancel the dialog:

public void onClick(DialogInterface arg0, int arg1) {
alertDialog.cancel();
startActivity(new Intent(YanivMenuActivity.this,YanivGameActivity.class).putExtra("reset", false));
                        }


alertbox.cancel();

This is what you should do when you want to close your alert box. Remember to replace alertbox with the name of your alertbox.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号