7

I am using Barcode Scanner Plugin for PhoneGap using ZXing as library project.

I have a code which works perfectly on Galaxy Tab 2 (7"). The same code doesn't work on Galaxy S3.

Problem : When ZXing CaptureActivity scans the barcode it just finish the CaptureActivity and Calling activity never comes back with onActivityResult method.

MainFest.

<activity
        android:name=".activity.MainActivity"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Black.NoTitleBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.google.zxing.client.android.CaptureActivity"
        android:configChanges="orientation|keyboardHidden"
        android:screenOrientation="landscape"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:windowSoftInputMode="stateAlwaysHidden" >
        <intent-filter>
            <action android:name="com.phonegap.plugins.barcodescanner.SCAN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

MainActivity.java

public void startActivityForResult(CordovaPlugin command, Intent intent,
        int requestCode) {
    this.activityResultCallback = command;
    this.activityResultKeepRunning = this.keepRunning;

    // If multitasking turned on, then disable it for activities that return
    // results
    if (command != null) {
        this.keepRunning = false;
    }

    // Start activity
    startActivityForResult(intent, requestCode);
}

protected void onActivityResult(int requestCode, int resultCode,
        Intent intent) {
    CordovaPlugin callback = this.activityResultCallback;
          if (callback != null) {
              callback.onActivityResult(requestCode, resultCode, intent);
          } else {
              Log.e(TAG, "Plugin callback null");
          }
      // else continue with any other code you need in the method

    super.onActivityResult(requestCode, resultCode, intent);


}

BarcodeScanner Plugin

private static final String SCAN_INTENT = "com.phonegap.plugins.barcodescanner.SCAN";
public void scan() {

    Intent intentScan = new Intent(SCAN_INTENT);
    intentScan.addCategory(Intent.CATEGORY_DEFAULT);
    this.cordova.startActivityForResult((CordovaPlugin) this, intentScan,
            AppConstants.CAMERA_SCAN_REQUEST_CODE);

}

I have ZXing projet as Library project.

Help would be appreciated.

3
  • I'm having the same issue, does anyone have a solution for this?
    – cush
    Commented Aug 13, 2013 at 23:41
  • having the same issue
    – dpaksoni
    Commented Feb 10, 2017 at 12:51
  • Somebody please answer my question. stackoverflow.com/q/42201805/2863013
    – dpaksoni
    Commented Feb 15, 2017 at 2:47

3 Answers 3

5

Per Cordova web view documentation

you need to have this code in your activity:

@Override
public void setActivityResultCallback(CordovaPlugin plugin) {
    this.activityResultCallback = plugin;        
}

public void startActivityForResult(CordovaPlugin command, Intent intent, int requestCode) {
    this.activityResultCallback = command;
    this.activityResultKeepRunning = this.keepRunning;

    // If multitasking turned on, then disable it for activities that return results
    if (command != null) {
        this.keepRunning = false;
    }

    // Start activity
    super.startActivityForResult(intent, requestCode);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    CordovaPlugin callback = this.activityResultCallback;
    if (callback != null) {
        callback.onActivityResult(requestCode, resultCode, intent);
    }
}

and in addition to:

this.cordova.startActivityForResult((CordovaPlugin) this, intentScan, AppConstants.CAMERA_SCAN_REQUEST_CODE);

you need to have the following method in your plugin:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    //do something with the result
    super.onActivityResult(requestCode, resultCode, intent);
}
3

try putting this cordova.setActivityResultCallback (this); just before calling the activity this.cordova.startActivityForResult((CordovaPlugin) this, intentScan, AppConstants.CAMERA_SCAN_REQUEST_CODE);

0

The answer that @aviv gave is absolutely correct, and though this may not have happened in this particular case, it is important to add one detail:

When setting the flags for your intent in your plugin, you must remember to set the flag as FLAG_ACTIVITY_SINGLE_TOP, instead of a FLAG_ACTIVITY_NEW_TASK, otherwise your setResult() method will not return as expected.

Also, remember to make sure your manifest declares the launchMode as such:

android:launchMode="singleTop"

There is a weird issue in Android where if you run as single top things seem to work fine, but if you run as single instance or single task the result will return immediately.

So please remember to add this when you are about to launch your native layer from your plugin

Intent i = new Intent(cordova.getActivity(), ThanksActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
cordova.startActivityForResult(this, i, 0);
PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
r.setKeepCallback(true);
callbackContext.sendPluginResult(r);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.