title: showing alert dialog on pressing back button
body: <p>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.        </p>
  
  <blockquote>
    <p>Here is my code.</p>
  </blockquote>
  
  <pre><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();
  
  }
  </code></pre>