Home » Android » android – how to use onActivityResult(..) if the activity is called from a menu

android – how to use onActivityResult(..) if the activity is called from a menu

Posted by: admin June 15, 2020 Leave a comment

Questions:

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!!

How to&Answers:

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();

Answer:

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.