title: Why is onActivityResult triggered before activity even starts?
body: <p>I have an app that needs to scan a barcode to get a code before it can continue. I use this code to start the scanning activity:</p>
  
  <pre><code>finish = (Button) findViewById(R.id.finishButton);
          finish.setOnClickListener(new OnClickListener() {
              public void onClick(View viewParam) {
  
                  /*Prompt the user to scan the barcode */
                  new AlertDialog.Builder(Visit.this)
                  .setMessage("Please Scan the clients barcode to complete the visit")
                  .setPositiveButton("Scan Barcode", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                          //Start the scan application
                          Intent intent = new Intent("com.google.zxing.client.android.SCAN");
                          startActivityForResult(intent, 0);
                      }
                  })
  
                  .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                          //Execute some method call
                          Toast.makeText(Visit.this, "Scan declined...", Toast.LENGTH_SHORT).show();
                      }
                  })
                  .show();
                  /* End of Scan prompt */
              }
          });
  </code></pre>
  
  <p>The above code sets a listener on a button labeled <code>finished</code>. When the button is clicked, it displays a prompt asking the user to scan a barcode or cancel.</p>
  
  <p>Clicking on the <kbd>Scan Barcode</kbd> button starts a new activity which starts the scan.</p>
  
  <p>I have the following code set up to read the result of the scan on the return from the scan:</p>
  
  <pre><code>/* Return from scanning barcode */
      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
  ;
        if (resultCode == Activity.RESULT_OK &amp;&amp; requestCode == 0) {
          Bundle extras = data.getExtras();
          String result = extras.getString("SCAN_RESULT");
        }
        Toast.makeText(Visit.this, "request code: "+requestCode+" result code = "+resultCode+ "\nRESULT_OK: "+Activity.RESULT_OK, Toast.LENGTH_SHORT).show();
      }
  </code></pre>
  
  <p>Which very simply (for now) outputs the activity result in a toast message.</p>
  
  <p>The problem I'm having is that the <code>onActivityResult</code> method is triggered as soon as I press the scan barcode button. </p>
  
  <p>The scanning process works fine as I can see the results of the scan in the logcat., however because it is triggered too soon the <code>onActivityResult</code> method never gets the scan result and the result code is always -1</p>
  
  <p>Am I missing a step here? is there someway of getting to <code>onActivityResult</code> to wait until the activity actual finishes?</p>