title: If back button pressed, alert show and application closes
body: <p>I have code for quiz, if <code>back button</code> pressed, <code>alert</code> show and application closes which is the flow of the application.
  But i have problem, if back button pressed, then it is not responding and executing the code.
  help me, thanks.</p>
  
  <p>public class GandaActivity  extends Activity {</p>
  
  <pre><code>List&lt;Question&gt; quesList;
  ArrayList&lt;Integer&gt; questIdRandom;
  int score=0;
  int qid=0;
  int randomIdSoal=0;
  Question currentQ;
  TextView txtQuestion;
  RadioButton rda, rdb, rdc;
  Button butNext;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.ganda_layout);
      DBHelper db=new DBHelper(this);
      quesList=db.getAllQuestions();
  
  
      Collections.shuffle(quesList);
  
      currentQ=quesList.get(qid);
      txtQuestion=(TextView)findViewById(R.id.tQuestion);
      rda=(RadioButton)findViewById(R.id.radioButton);
      rdb=(RadioButton)findViewById(R.id.radioButton2);
      rdc=(RadioButton)findViewById(R.id.radioButton3);
      butNext=(Button)findViewById(R.id.button_next);
      setQuestionView();
      butNext.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              RadioGroup grp=(RadioGroup)findViewById(R.id.groupRadio);
              RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
  
              if(grp.getCheckedRadioButtonId() == -1){
  
              }
              else if(grp.getCheckedRadioButtonId() != -1) {
                  grp.clearCheck();
                  if (currentQ.getANSWER().equals(answer.getText())) {
                      //Log.d("yourans", currentQ.getANSWER()+" "+answer.getText());
                      score++;
                      Log.d("score", "Your score" + score);
                  }
                  if (qid &lt; 10) {
                      currentQ = quesList.get(qid);
                      setQuestionView();
                  } else {
                      Intent intent = new Intent(GandaActivity.this, ResultQuizGanda.class);
                      Bundle b = new Bundle();
                      b.putInt("score", score); //Your score
                      intent.putExtras(b); //Put your score to your next Intent
                      startActivity(intent);
                      finish();
                  }
              }
  
          }
      });
  }
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;
  }
  private void setQuestionView()
  {
      txtQuestion.setText(currentQ.getQUESTION());
      rda.setText(currentQ.getOPTA());
      rdb.setText(currentQ.getOPTB());
      rdc.setText(currentQ.getOPTC());
      qid++;
  }
  
  private void setRandomSoal(){
      int size = quesList.size();
      int sizeRandom = 0;
      Log.d("TOTAL SOAL","Jumlah soal "+size);
  
      while(sizeRandom&lt;10){
          int id = new Random().nextInt(size);
          if(sizeRandom==0){
              questIdRandom.add(id);
          }else if(cekQuestRandomId(id)){
              questIdRandom.add(id);
          }
          sizeRandom = questIdRandom.size();
      }
  
  
  }
  
  private boolean cekQuestRandomId(int id){
      boolean cek = false;
  
      int sizeRandom = 0;
      int i = 0;
      while(sizeRandom&lt;10){
          if(sizeRandom==0){
              cek = false;
          }else if(questIdRandom.get(i)==id){
              cek = false;
          }else{
              cek = true;
          }
          sizeRandom = questIdRandom.size();
          i++;
      }
      return cek;
  }
  public boolean onKeyDown(int keyCode, KeyEvent event){
      if (keyCode == KeyEvent.KEYCODE_BACK &amp;&amp; event.getRepeatCount() == 0){
          AlertDialog.Builder builder = new AlertDialog.Builder(this);
          builder.setMessage("Apakah anda yakin ingin keluar?")
                  .setCancelable(false)
                  .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                          finish();
                      }
                  })
                  .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                          dialog.cancel();
                      }
                  });
  
          return true;
      }
      return super.onKeyDown(keyCode, event);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>thanks, please help me!</p>