0

So I have RegisterActivity.java, where after user click register, it should call ScanActivity() method in ScanActivity.java.

In ScanActivity(), it will start scanning activity, and return the scan result back to RegisterActivity.java.

Using the scan result, the validation of user will occur in RegisterActivity.java.

How to do this?

In RegisterActivity.java

// call ScanActivity
ScanActivity scanActivity = new ScanActivity();

// get scan result from ScanActivity
String scanResult = scanActivity.ScanActivity();

// compare with useridtxt
// if useridtxt same with scan result, start save into parse
if (useridtxt.equals(scanResult)) {
    // some code here
} else if (!useridtxt.equals(scanResult)) {
    // some code here
} else {
    // some code here
}

In ScanActivity.java

public class ScanActivity extends Activity {

public static final int CODE39 = 39;
private static final int ZBAR_SCANNER_REQUEST = 0;

String ScanActivity() {

    Intent intent = new Intent(this, ZBarScannerActivity.class);
    intent.putExtra(ZBarConstants.SCAN_MODES, new int[]{Symbol.CODE39});
    startActivityForResult(intent, ZBAR_SCANNER_REQUEST);
    String scanResult = getIntent().getStringExtra(ZBarConstants.SCAN_RESULT);
    return scanResult;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (resultCode == RESULT_OK)
    {
        // maybe save the scan result here, and pass it to String scanResult in RegisterActivity.java?
    } else if(resultCode == RESULT_CANCELED) {
        Toast.makeText(this, "Camera unavailable", Toast.LENGTH_SHORT).show();
    }
}
August
  • 309
  • 1
  • 5
  • 15
  • have you put onActivityResult in wrong class or you have printed it wrong? – therealprashant Oct 30 '15 at 19:18
  • **Don't do this** `ScanActivity scanActivity = new ScanActivity();`. Use the activity lifecycle. Start them with Intents and override the activity methods. [Return to previous activity with different data](http://stackoverflow.com/questions/18243515/android-going-back-to-previous-activity-with-different-intent-value/18243541#18243541) – codeMagic Oct 30 '15 at 19:23
  • @therealprashant I guess it's already correct as it is for intent in ScanActivity() ? – August Oct 30 '15 at 19:56
  • @codeMagic okay but now how do I assign the result of scanning to `scanResult`, after `startActivityForResult()` ? – August Oct 31 '15 at 05:35

1 Answers1

0

This is quite simple. A sample code can run like below

startActivityForResult(Intent intent, int requestCode); //Fill intent with your desired class.

In the target class(ScanActivity), don't forget to put the results back

setResult (int resultCode, Intent data) //in data bundle put all your required output

This way you can extract the matching data with expected result code.

And in caller class now

protected void onActivityResult (int requestCode, int resultCode, Intent data) {} //Your logic to manipulate data
Amit Kumar
  • 391
  • 3
  • 10