0

I added an alert dialogue into my app and i want to user when he press yes he should exit from the complete app but it dose not. by pressing yes i just come back to previous activity here is code

 @Override
public void onBackPressed() {
    new AlertDialog.Builder(Chapter1.this)
            .setTitle("Exit")
            .setIcon(R.drawable.ic_baseline_warning_24)
            .setMessage("Are you sure you want to go back")
            .setPositiveButton("yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();

                }
            }).setNeutralButton("Help", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(Chapter1.this, "This is an \"Android Studio\" learning app", Toast.LENGTH_SHORT).show();
        }
    }).setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();

        }
    }).show();

can any one tell me which kind of change in code should i made to that when i press yes i should leave the app instead of going to previous activity.

1 Answers1

0

finish(); also try looking at the response that was provided here How to quit android application programmatically you will get some clue.

  @Override
  public void onBackPressed() {
   new AlertDialog.Builder(Chapter1.this)
        .setTitle("Exit")
        .setIcon(R.drawable.ic_baseline_warning_24)
        .setMessage("Are you sure you want to go back")
        .setPositiveButton("yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                    finishAffinity();
                    System.exit(0);

            }
        }).setNeutralButton("Help", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(Chapter1.this, "This is an \"Android Studio\" learning 
   app", Toast.LENGTH_SHORT).show();
    }
   }).setNegativeButton("No", new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();

    }
}).show();
The Codepreneur
  • 236
  • 3
  • 12