OnActivityResult Never Call Phonegap Cordova

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

I have a code that works fine on the Galaxy Tab 2 (7 "). The same code does not work on the Galaxy S3.

Problem: When ZXing CaptureActivity scans a barcode, it simply finishes the CaptureActivity and Calling operation, which is never returned using the 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 a ZXing project as a library project.

Help will be appreciated.

+7
android android-intent cordova barcode-scanner zxing
source share
4 answers

Per Cordoba Web Viewer 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); 

Your plugin should have the following method:

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

try to put this cordova.setActivityResultCallback (this); just before the operation call this.cordova.startActivityForResult((CordovaPlugin) this, intentScan, AppConstants.CAMERA_SCAN_REQUEST_CODE);

+2
source share

In my cordova plugin class, before I call the startActivityForResult method, I set this class as the result callback.

 cordova.setActivityResultCallback(this); 
+1
source share

The answer given by @aviv is absolutely correct, and although this may not happen in this particular case, it is important to add one detail:

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

Also, be sure to make sure your manifest declares startMode as such:

 android:launchMode="singleTop" 

Android has a strange problem: if you run as one top, everything works fine, but if you run as one instance or a separate task, the result will be returned immediately.

Therefore, please do not forget to add this when you are about to start your own 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); 
0
source share

All Articles