Phil Adams
Phil Adams

Reputation: 105

Can't get Android App to Exit

I'm struggling to get my android app to exit via the alertdialog.

Clicking 'yes' to play again works fine as in it restarts the app and you get to go again.

Unfortunately, when I click 'no' it just removes the dialog alert box and I can't figure out why.

Any help greatly appreciated.

Thanks

@Override
       public void onClick(View v) {
           AlertDialog.Builder alert = new    AlertDialog.Builder(Task1Activity.this);
           alert.setMessage("You have guessed incorrectly three times. " +
               "The answer was " + ranNum + ". " + "Would you like to play again?")
                   .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
    public void onClick(DialogInterface dialog, int which) {

    Intent i = new Intent(Task1Activity.this, Task1Activity.class);
    startActivity(i);

    }
});

alert
.setCancelable(false)
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Task1Activity.this.finish();
};
)
    .setTitle("Unlucky!")
    .create();

Upvotes: 1

Views: 52

Answers (3)

AtaerCaner
AtaerCaner

Reputation: 712

I think problem is startActivity(i); When you click on the ok, startActivity(i) starts another Task1Activity on current Task1Activity and when you press the cancel, the top Task1Activity is finishing and you go back the previous Task1Activity. Try startActivity(i) with flag.

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

Upvotes: 0

rafsanahmad007
rafsanahmad007

Reputation: 23881

you are creating the Activity Intent i = new Intent(Task1Activity.this, Task1Activity.class); which added the activity on stack.

try this code:

 final AlertDialog.Builder alertDialog = new AlertDialog.Builder(
        Task1Activity.this);
alertDialog.setTitle("Unlucky!");
alertDialog.setCancelable(false)
alertDialog.setMessage("You have guessed incorrectly three times. " +
           "The answer was " + ranNum + ". " + "Would you like to play again?");
alertDialog.setPositiveButton("YES",
        new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i)        {
                 Intent i = new Intent(Task1Activity.this, Task1Activity.class);
                 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                startActivity(i);
            }
        });
alertDialog.setNegativeButton("NO",
        new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                alertDialog.dismiss();   
                //getActivity().finish();
                finishAffinity();   //finish all previous activity on stack
            }
        });
alertDialog.show();

Upvotes: 1

Hanan Rofe Haim
Hanan Rofe Haim

Reputation: 918

You keep adding activities on top of each other every time you do:

DialogInterface.OnClickListener() {


    @Override
    public void onClick(DialogInterface dialog, int which) {

    Intent i = new Intent(Task1Activity.this, Task1Activity.class);
    startActivity(i);

    }
});

In order to keep only 1 activity alive you should finish() the old one when you start a new activity:

DialogInterface.OnClickListener() {


    @Override
    public void onClick(DialogInterface dialog, int which) {

    Intent i = new Intent(Task1Activity.this, Task1Activity.class);
    startActivity(i);
    finish();
    }
});

Upvotes: 0

Related Questions