开发者

Alert box ..on and off

开发者 https://www.devze.com 2023-04-02 14:34 出处:网络
Hi I am using alertdialog..and when the user clicks ok it has to restart the same activty (GAME) and when he clicks no it has to go to the main menu.. but when i click ok..2 activities

Hi I am using alertdialog..and when the user clicks ok it has to restart the same activty (GAME) and when he clicks no it has to go to the main menu.. but when i click ok..2 activities are running simultaneously and when i clock no..and come back here..the dialog is still present! Help! This is the snippet

  AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
    alertbox.setMessage("oops! wrong answer! wanna play again?");
    alertbox.setPositiveButton("Yea sure!",
            new DialogInterface.OnClickListener() {
                                   Intent game = new Intent("nik.trivia.GAME");
                    startActivity(game);
                    finish();
});





alertbox.setNegativeButton("Nope! take me to the main menu",
            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface arg0, int arg1) {
                    Intent menu = ne开发者_如何转开发w Intent("nik.trivia.MENU");
                    startActivity(menu);
                    finish();


                }
            });


You need to call dismiss() on your AlertDialog after the user has clicked a button.

EDIT: just place this line of code:

   arg0.dismiss();

inside the onClick method.


In order to start only an instance of this activity you have to set the flag as singleTask and get the intent in the onNewIntent() method.

Edited:

Set singleTask as flag of the activity in the manifest file in order not to have many instances of the same activity and add this to your activity:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);   
    setIntent(intent);
    // Do here whatver you want. This method is similar to the onCreate() method.
}


in your case you can add: alertbox.dismiss()

when you call finish() it is on the whole activity not just the dialog

EDIT:

  public void onClick(DialogInterface arg0, int arg1) {
                Intent menu = new Intent("nik.trivia.MENU");
                startActivity(menu);

                arg0.dismiss();
                finish(); 

or you can juste declare alertbox protected:

protected AlertDialog alertDialog 

and the n you will be able to call dismis on it from everywhere

0

精彩评论

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

关注公众号