title: How to finish an android application?
body: <p>I need to finish an android application. For that i wrote </p>
  
  <pre><code>@Override
  public void onBackPressed() {
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setMessage("Are you sure You want to exit")
          .setCancelable(false)
          .setPositiveButton("YES"),
          new DialogInterface.OnClickListener() {
              // On
              // clicking
              // "Yes"
              // button
  
              public void onClick(DialogInterface dialog,int id) {
                  System.out.println(" onClick ");
                  closeApplication(); // Close Application method called
              }
          })
          .setNegativeButton("NO"),
          new DialogInterface.OnClickListener() {
              // On
              // clicking
              // "No"
              // button
              public void onClick(DialogInterface dialog,int id) {
                  dialog.cancel();
              }
          });
  
          AlertDialog alert = builder.create();
          alert.show();
      }
  
      private void closeApplication() {
          System.out.println("closeApplication ");
          this.finish();
      }
  }
  </code></pre>
  
  <p>But if any activity is not finished in the application, when i tried to exit the application that activity is finished first and the application is not exiting.. i tried 2 times to exit this application... How i can finish all the activities in an application when i need to exit... or is there any way to finish the entire application</p>