how to use onActivityResult(..) if the activity is

2019-02-12 19:20发布

问题:

This is my problem:

class main extends menuActivity{
  //
  ..
  //
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
     if (resultCode == 0) 
        camera_barcode = INTENT.getStringExtra("SCAN_RESULT");
     }
  }
}

the INTENT is created in the menuActivity class:

public class menuActivity extends Activity {

    public INTENT;
@Override
public boolean onCreateOptionsMenu(Menu menu) {

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

       //
       INTENT = new Intent("com.google.zxing.client.android.SCAN");
       INTENT.putExtra("SCAN_MODE", "QR_CODE_MODE");
       startActivityForResult(INTENT, 0);
       //
    }
} 

The problem is that the String camera_barcode is null, can't understand why.

Q: if instead of QR_CODE_MODE I want to scan 1d barcodes? R: cameraScan.putExtra("SCAN_MODE", "PRODUCT_MODE");

Thank you!!

回答1:

Finish the activity you are starting for result like this

  Bundle b = new Bundle();
  b.putString(key, value);
  Intent i = getIntent(); //gets the intent that called this intent
  i.putExtras(b);
  setResult(Activity.RESULT_OK, i);
  finish();


回答2:

It doesn't matter where you create the intent. If you've used Activity's startActivityForResult() method, then you'll receive results in onActivityResult() function.