title: back button error, action down
body: <p>I have a problem with my code about back button. I've tried a lot of answers here in this site.
  I have the mainActivity that calls a second activity with startActivityforResult. This second activity starts bluetooth and show a list of the bonded devices, but if I press the backbutton it stops the app with an error.</p>
  
  <pre><code>public class Main extends Activity implements OnSeekBarChangeListener{
  ...
  
  @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          String address = data.getExtras().getString(BondedDevices.DEVICE_ADDRESS);
          if (resultCode==Activity.RESULT_CANCELED){
              Toast.makeText(getApplicationContext(), "Finalizando dispositivos pareados", Toast.LENGTH_SHORT).show();
              return;
          }
          switch (requestCode) {
          case DISPOSITIVOS_PAREADOS:
              if(resultCode==Activity.RESULT_OK){
                  mConnectThread = new ConnectThread(address);
                  mConnectThread.start();
                  estado = EST_CONECTADO;   //informa que esta conectado
                  atualizaEstado();
                  break;
              } 
          return;
          }
      }
  </code></pre>
  
  <p>But when I am in the second activity, and try to back to the mainactivity just pressing the back button, I get an error on main activity and my app return an error:</p>
  
  <pre><code>public class BondedDevices extends ListActivity {
  ....
  
  @Override
      public void onBackPressed() {
          // TODO Auto-generated method stub
          if(D) Log.e(TAG, "+++ ON BACK PRESSED +++");
          setResult(Activity.RESULT_CANCELED);
          this.finish();
      }
  </code></pre>
  
  <p>or like this:</p>
  
  <pre><code>@Override
      public boolean onKeyDown(int keyCode, KeyEvent event) {
          if(event.getAction() == KeyEvent.ACTION_DOWN)
          {
              switch(keyCode)
              {
              case KeyEvent.KEYCODE_BACK:
                  Intent returnIntent = new Intent();
                  setResult(Activity.RESULT_CANCELED, returnIntent);
                  finish();
                  return true;
              }
          }
  
          return false; 
      }
  </code></pre>
  
  <p>I have tried a lot of different code, but it still doesn't work. Please someone help me. Thank you.</p>