title: Android: creating alert dialog when back button is pressed
body: <p>I have added the below code so if back button is pressed in the main activity it shows a alert dialog with two buttons to make a final decision...but sometimes when i click on <em>YES</em> (a positive button to finish the activity) the same activity shown again instead of finishing the activity, i want  to know whether there is any mistake in my code...</p>
  
  <p>MainActivity.java</p>
  
  <pre><code>public void onBackPressed() {
  
          //super.onBackPressed();
          Log.d("back button", "back button pressed");
          AlertDialog.Builder ad1=new AlertDialog.Builder(this);
          ad1.setMessage("Are you sure you want to exit? ");
          ad1.setCancelable(false);
  
  
          ad1.setNegativeButton("NO", new DialogInterface.OnClickListener() {             
              @Override
              public void onClick(DialogInterface arg0, int arg1) {   
  
  
              }
          });
  
          ad1.setPositiveButton("YES", new DialogInterface.OnClickListener() {                
              @Override
              public void onClick(DialogInterface arg0, int arg1) {   
                  finish();
              }
          });
          AlertDialog alert=ad1.create();
          alert.show();   
  
              }
  </code></pre>
  
  <p>MainActivity.java complete code</p>
  
  <pre><code>package com.vibrator;
  
  import android.annotation.SuppressLint;
  import android.app.AlertDialog;
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.graphics.Color;
  import android.graphics.drawable.ColorDrawable;
  import android.os.Bundle;
  import android.os.Vibrator;
  import android.support.v7.app.ActionBar;
  import android.support.v7.app.ActionBarActivity;
  import android.util.Log;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.Button;
  import android.widget.CompoundButton;
  import android.widget.CompoundButton.OnCheckedChangeListener;
  import android.widget.SeekBar;
  import android.widget.SeekBar.OnSeekBarChangeListener;
  import android.widget.Spinner;
  import android.widget.TextView;
  import android.widget.ToggleButton;
  
  @SuppressLint("ShowToast")
  public class MainActivity extends ActionBarActivity implements OnCheckedChangeListener, OnSeekBarChangeListener{
  
      ToggleButton tbutton;
      Vibrator v;
      int i=400,j=50;
      int k,l;
      TextView txt;
      SeekBar sb;
      TextView txt1;
      SeekBar sb1;
      Spinner spinner1;
      Button btn;
  
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          ActionBar bar = getSupportActionBar();
          bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#336699")));
  
          //Log.e("values","i value at oncreate "+i);
          //Log.e("values","i value at oncreate  "+j);
          //creating instance for vibrator
           v=(Vibrator) getSystemService(VIBRATOR_SERVICE);        
           //checking if the device has vibrator or not        
          if(v!=null){
  
              setContentView(R.layout.activity_main);
  
          }
          else {
              setContentView(R.layout.secound);
          }
              //Log.e("vibrate", "setcontentview");
  
  
          //creating instance of toggle buttton and setting listener
          tbutton=(ToggleButton) findViewById(R.id.tb1);
          tbutton.setOnCheckedChangeListener(this);
  
  
          //creating instance and setting onclicklistener for the button
          /*btn=(Button) findViewById(R.id.button);
          btn.setOnClickListener(this);*/
  
          sb=(SeekBar) findViewById(R.id.seekbar);
          txt=(TextView) findViewById(R.id.txt12);
          sb1=(SeekBar) findViewById(R.id.seekbar1);
          txt1=(TextView) findViewById(R.id.txt11);
  
  
          sb1.setOnSeekBarChangeListener(this);       
          sb.setOnSeekBarChangeListener(this);
  
  
  
  
      }
  
      @Override
      public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
          //Log.d("real status", ""+arg1);
          //if vibrator is ON
          if(arg1)
          {
              Log.e("vibrate", "true vibrate");
  
              // Start without a delay
              // Vibrate for 100 j milliseconds
              // Sleep for 1000  (i) milliseconds 
              Log.e("values","i value "+i);
              Log.e("values","i value "+j);
              long pattern[] = {0, j, i};
              // The '0' here means to repeat indefinitely
              // '-1' would play the vibration once                       
              v.vibrate(pattern, 0);          
          }
  
          else {
  
              Log.e("vibrate", "false no v");
              //Toast.makeText(this,"status: "+arg1, Toast.LENGTH_SHORT).show();
              v.cancel();         
          }       
      }
  
  
      public void onProgressChanged(SeekBar s, int v, boolean b) {
  
          //switch statement to update values of seekbars
                  switch (s.getId()) {
                  case R.id.seekbar:
                      k=v;
                      txt.setText("Vibrator OFF time: "+k);
                      //Log.e("values","seekbar value "+k);
                      break;
  
                  case R.id.seekbar1:
                      l=v;
                      txt1.setText("Vibrator ON time: "+l);
                      //Log.e("values","seekbar1 value "+l);
                      break;
  
                  }   
          //seekbarchanged method implementation
          if (tbutton.isChecked()) {
              Log.e("values","yes tbutton is checked");   
          tbutton.setChecked(false);
          switch (s.getId()) {
          case R.id.seekbar:
              i=k;            
              //Log.e("true", "seekbar tbutton checked "+i);
              break;
  
          case R.id.seekbar1:
              j=l;
              //Log.e("true", "seekbar1 tbutton checked "+j);
              break;      
          }       
          Log.e("true", "set true");
          tbutton.setChecked(true);
  
      }       
          else{
              //Toast.makeText(this,"Set ON to customize", Toast.LENGTH_SHORT).show();
              AlertDialog.Builder ad=new AlertDialog.Builder(this);
              ad.setTitle("Instruction");
              ad.setMessage("Set the button ON to customize");
              ad.setNeutralButton("OK", new DialogInterface.OnClickListener() {               
                  @Override
                  public void onClick(DialogInterface arg0, int arg1) {                   
                  }
              });
              ad.show();
          }
  
  
      }
  
      @Override
      public void onStartTrackingTouch(SeekBar arg0) {        
      }
      @Override
      public void onStopTrackingTouch(SeekBar arg0) {     
      }
  
  
      @Override
  
  
  public boolean onCreateOptionsMenu(Menu menu) {
  
          MenuInflater inflate= getMenuInflater();
          inflate.inflate(R.menu.menu_v, menu);
          return super.onCreateOptionsMenu(menu);
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
  
          switch (item.getItemId()) {
          case R.id.menu_setting:     
              break;
  
          case R.id.about:
              //Toast.makeText(this, "about clicked", Toast.LENGTH_SHORT).show();
              AlertDialog.Builder ad=new AlertDialog.Builder(this);
              ad.setTitle("About");
              ad.setMessage("This app is developed by Rajesh Uragonda");
              ad.setNeutralButton("OK", new DialogInterface.OnClickListener() {               
                  @Override
                  public void onClick(DialogInterface arg0, int arg1) {                   
                  }
              });
              ad.show();          
              break;  
  
          case R.id.settings:
              //Toast.makeText(this, "settings clicked", Toast.LENGTH_SHORT).show();
              AlertDialog.Builder ad2=new AlertDialog.Builder(this);
              ad2.setTitle("Settings");
              ad2.setMessage("Your settings here");
              ad2.setNeutralButton("OK", new DialogInterface.OnClickListener() {              
                  @Override
                  public void onClick(DialogInterface arg0, int arg1) {                   
                  }
              });
              ad2.show(); 
              break;
          case R.id.help:
              //Toast.makeText(this, "help clicked", Toast.LENGTH_SHORT).show();
              AlertDialog.Builder ad1=new AlertDialog.Builder(this);
              ad1.setTitle("Help");
              ad1.setMessage(R.string.alerthelp);
              ad1.setNeutralButton("OK", new DialogInterface.OnClickListener() {              
                  @Override
                  public void onClick(DialogInterface arg0, int arg1) {                   
                  }
              });
              ad1.show();
  
  
              break;
  
  
  
          }
          return super.onOptionsItemSelected(item);
  
  
  }
  
      @Override
      protected void onPause() {
          // TODO Auto-generated method stub
  
          super.onPause();
          if (tbutton.isChecked()) {
              tbutton.setChecked(false);
              Log.d("onpause", "onpause mainactivity tbutton");
          }
          Log.d("onpause", "onpause mainactivity");
  
      }
  
  
      //responds to the button at top used as navigation to next activity
      public void btn(View v){
          //Button btn1=(Button) findViewById(R.id.btn1);
          Intent i = new Intent(MainActivity.this, CustomPatterns.class);
          startActivity(i);
          }
  
      //when back button pressed it goes directly to home
      public void onBackPressed() {
  
          //super.onBackPressed();
          Log.d("back button", "back button pressed");
          AlertDialog.Builder ad1=new AlertDialog.Builder(this);
          ad1.setMessage("Are you sure you want to exit? ");
          ad1.setCancelable(false);
  
  
          ad1.setNegativeButton("NO", new DialogInterface.OnClickListener() {             
              @Override
              public void onClick(DialogInterface arg0, int arg1) {   
  
  
              }
          });
  
          ad1.setPositiveButton("YES", new DialogInterface.OnClickListener() {                
              @Override
              public void onClick(DialogInterface arg0, int arg1) {   
  
                  MainActivity.this.finish();
              }
          });
          AlertDialog alert=ad1.create();
          alert.show();   
  
                  }
  
              }
  </code></pre>