0

I want to show an alert message on pressing back button of Android device asking user weather he is sure or not. If user press "Yes" then he should navigate to previous. in case of "NO" activity should resume. But the problem occurs when I press back button it performs both the actions. It also navigate to previous activity and displays the alert dialog.

Here is my code.

public void onBackPressed() {

    super.onBackPressed();

    AlertDialog.Builder alertdialog=new AlertDialog.Builder(this);
    alertdialog.setTitle("Warning");
    alertdialog.setMessage("Are you sure you Want to exit the tutorial???");
    alertdialog.setPositiveButton("yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent intent=new Intent(secAddition.this,addition.class);
            startActivity(intent);
            secAddition.this.finish();
        }
    });

    alertdialog.setNegativeButton("No", new DialogInterface.OnClickListener(){
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    AlertDialog alert=alertdialog.create();
    alertdialog.show();

}
Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
Waqas Ahmed
  • 75
  • 2
  • 9
  • dont call startActivity on BackPress. you should call super OnBackPress. when hardware back is pressed. check my answer. – Developine May 17 '16 at 12:38

5 Answers5

10

If you want to show the alert dialog without closing the app or clearing the stack, you should not call super.onBackPressed();

Seishin
  • 1,493
  • 1
  • 19
  • 30
3

Please check the following code snippet

 @Override
 public void onBackPressed() {
     //super.onBackPressed();
     IsFinish("Want to close app?");
 }

 public void IsFinish(String alertmessage) {

      DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                    switch (which) {
                        case DialogInterface.BUTTON_POSITIVE:
                            android.os.Process.killProcess(android.os.Process.myPid());
                            // This above line close correctly
                            //finish();
                            break;

                        case DialogInterface.BUTTON_NEGATIVE:  
                            break;
                    }
                }
            };

      AlertDialog.Builder builder = new AlertDialog.Builder(context);
      builder.setMessage(alertmessage)
                .setPositiveButton("Yes", dialogClickListener)
                .setNegativeButton("No", dialogClickListener).show();

 }
Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
MabrurChowdhury
  • 375
  • 1
  • 3
  • 13
2

Remove

super.onBackPressed();

This should work.

public void onBackPressed() {

   AlertDialog.Builder alertdialog=new AlertDialog.Builder(this);
   alertdialog.setTitle("Warning");
   alertdialog.setMessage("Are you sure you Want to exit the tutorial???");
   alertdialog.setPositiveButton("yes", new DialogInterface.OnClickListener(){
       @Override
       public void onClick(DialogInterface dialog, int which) {
           Intent intent=new Intent(secAddition.this,addition.class);
           startActivity(intent);
           secAddition.this.finish();
       }
    });

   alertdialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
       }
   });

   AlertDialog alert=alertdialog.create();
   alertdialog.show();

}
Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
2

This is what you are looking for. you don't need to startActivity on back pressed. If user select Yes than call super onBackPress

public void onBackPressed() {

   AlertDialog.Builder alertdialog=new AlertDialog.Builder(this);
   alertdialog.setTitle("Warning");
   alertdialog.setMessage("Are you sure you Want to exit the tutorial???");
   alertdialog.setPositiveButton("yes", new DialogInterface.OnClickListener(){

      @Override
      public void onClick(DialogInterface dialog, int which) {
         super.onBackPressed();
      }
    });

    alertdialog.setNegativeButton("No", new DialogInterface.OnClickListener(){

      @Override
      public void onClick(DialogInterface dialog, int which) {
         dialog.cancel();
      }

    });

    AlertDialog alert=alertdialog.create();
    alertdialog.show();

}
Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
Developine
  • 12,483
  • 8
  • 38
  • 42
1

you can write like this for your code to work :

public void onBackPressed() {

     AlertDialog.Builder alertdialog=new AlertDialog.Builder(this);
     alertdialog.setTitle("Warning");
     alertdialog.setMessage("Are you sure you Want to exit the tutorial???");
     alertdialog.setPositiveButton("yes", new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialog, int which) {
                 super.onBackPressed();    
           }
     });

    alertdialog.setNegativeButton("No", new DialogInterface.OnClickListener(){
           @Override
           public void onClick(DialogInterface dialog, int which) {
                 dialog.cancel();
           }
    });

    AlertDialog alert=alertdialog.create();
    alertdialog.show();

}

No need to start the Activity again if you are going to previous Activity,

In your code when you do a back press, super.onBackPressed() gets called so it goes to previous Activity

Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
Satyen Udeshi
  • 3,223
  • 1
  • 19
  • 26