I want to Scan a Barcode inside my Fragment. If I click on the Scan button the barcode Scanner pops up and scans the Barcode. But I cannot get the scanned text. Whats the problem with my onActivityResult method?
Background: Ive got two different scan buttons in my application. the first one scans qrcodes and is located in my MainActivity
. It works fine and also my onActivityResult
method works good.
The second scan button is located in my AddDataFragment. With this button I want to scan Serials, based on regular barcodes. So I have 2 scan events and one onActivityResult
....
EDIT: I changed my old false Code to my (hopefully) better Code.
This is the onActivityResult
Code in my MainActivity
. How do I get the scanned string (scannedResult) from my MainActivity
to my AddDataFragment
?
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String scanContent = intent.getStringExtra("SCAN_RESULT");
String scanFormat = intent.getStringExtra("SCAN_RESULT_FORMAT");
Log.d("INHALT", scanContent);
Log.d("FORMAT", scanFormat);
if (scanFormat == "QR_CODE"){
String [] Split = scanContent.split("\\s");
String product = Split[0];
String label = Split[1];
mydb.insertData(product, label);
} else {
scannedResult = scanContent;
}
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
Fragment fragment;
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
fragment = new ListViewFragment();
ft.replace(R.id.container, fragment);
ft.commitAllowingStateLoss();
}
This is the method I call in my AddDataFragment
inputSerial = (EditText) view.findViewById(R.id.editText_serial);
ImageButton ib = (ImageButton) view.findViewById(R.id.button_scan_serial);
ib.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_FORMATS", "CODE_39,CODE_93,CODE_128,DATA_MATRIX,ITF,CODABAR,EAN_13,EAN_8,UPC_A");
startActivityForResult(intent, 0);
}
});
But it seems that the call of onActivityResult
isnt working because I dont get the Log entrys after scanning a regular Barcode. (No crash or Error Message)