2

I'm using zxing to read QR codes. It works fine when I scan a code when the phone is in portrait mode, but when I scan in landscape mode it appears to return, but the onActivityResult method is never called (my breakpoint isn't even hit).

From build file:

compile 'com.journeyapps:zxing-android-embedded:3.0.2@aar'
compile 'com.google.zxing:core:3.2.0'

Code from the activity. This is called but at that point fragment is null:

@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    if (fragment != null && Activity.RESULT_OK == resultCode) {
        fragment.onActivityResult(requestCode, resultCode, intent);
    }
}

Here is my code from the fragment:

@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {

    // Handle QR scan result
    if (Activity.RESULT_OK == resultCode) {
        final IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (scanResult != null) {
            final String contentString = scanResult.getContents();
            if (Strings.isNotEmpty(contentString)) {
                processDataAsynchronously(contentString);
            }
        }
    }
}

OnClick code:

@Override
public void onClick(final View v) {

    int viewId = v.getId();

    if (viewId == R.id.check_qr_button) {

        // Get permissions to use the camera
        if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

            if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.CAMERA)) {

                final Runnable cameraPermissionsRunnable = new Runnable() {
                    @Override
                    public void run() {
                        ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
                    }
                };

                // Why we request this permission
                DialogManager.showOkDialog(getActivity(), R.string.permissions_explanation_camera_qr, true, cameraPermissionsRunnable, true, true);

            } else {

                ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);

            }
        } else {


            IntentIntegrator.forSupportFragment(this).initiateScan();

            //ALSO TRIED THIS BUT DIDN'T WORK EITHER
             /*
            final IntentIntegrator integrator = new IntentIntegrator(getActivity());
            integrator.setOrientationLocked(false);
            integrator.setBeepEnabled(true);
            integrator.initiateScan();
            */

        }
        return;
    }

    super.onClick(v);
}

Thanks for any tips!

zundi
  • 2,361
  • 1
  • 28
  • 45
  • Does your `Acitivity` has `android:configChanges="orientation"` declared in Manifest? If so, then it might be issue with orientation change and `Activity` restoration. Try to put breakpoints in other `Activity` lifecycle methods, like `onStart()` and `onResume()` and see, what happens. – R. Zagórski Jan 29 '18 at 14:07
  • Hi @R.Zagórski . Yes actually that's a good idea, I hadn't thought of that. If I start the scan in portrait, then move to landscape, then scan and return, the activity is recreated (as expected). Is there any way to capture the scanned QR code, given that the `onActivityResult()` method isn't called in this scenario? Or would the solution be to automatically attempt to rescan the QR code in this new configuration? – zundi Jan 30 '18 at 14:39
  • From your code I assume, that `Fragment` is calling `startActivityForResult()`. If you add fragment programatically to Activity's view, please do a quick test and don't recreate it when Activity's `onCreate(Bundle savedInstanceState)` method has `savedInstanceState` being not `null`. `Fragment` should be automatically added as the same instance. And also verify that `Activity`'s `onActivityResult()` is called. – R. Zagórski Jan 30 '18 at 17:13
  • Hi @R.Zagórski. My fragment isn't calling `startActivityForResult ()`, but instead it's using `IntentIntegrator.forSupportFragment(this).initiateScan();` The activity's onActivityResult() is called. – zundi Feb 01 '18 at 16:45
  • @R.Zagórski I've updated the question to include the activity's `onActivityResult() ` – zundi Feb 01 '18 at 17:01
  • To begin with can you try updating the Zxing version to latest `3.5.0` and see if it still occurs : `compile 'com.journeyapps:zxing-android-embedded:3.5.0' ` `compile 'com.google.zxing:core:3.3.1'`? – Shobhit Puri Feb 03 '18 at 00:06
  • @ShobhitPuri Tried updating but that didn't change this. – zundi Feb 05 '18 at 21:12

0 Answers0