Nikhil
Nikhil

Reputation: 2318

Alert box ..on and off

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 = new Intent("nik.trivia.MENU");
                    startActivity(menu);
                    finish();


                }
            });

Upvotes: 0

Views: 183

Answers (3)

vallllll
vallllll

Reputation: 2761

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

Upvotes: 0

Ovidiu Latcu
Ovidiu Latcu

Reputation: 72321

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.

Upvotes: 1

Dimitris Makris
Dimitris Makris

Reputation: 5183

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.
}

Upvotes: 0

Related Questions