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!