Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onActivityResult Never gets called Phonegap Cordova

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.

like image 596
ValayPatel Avatar asked Aug 08 '13 11:08

ValayPatel


2 Answers

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

like image 42
Beelphegor Avatar answered Nov 09 '22 18:11

Beelphegor


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);
}
like image 117
aviv Avatar answered Nov 09 '22 19:11

aviv