2

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 onActivityResultmethod 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 onActivityResultCode in my MainActivity. How do I get the scanned string (scannedResult) from my MainActivityto 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 onActivityResultisnt working because I dont get the Log entrys after scanning a regular Barcode. (No crash or Error Message)

Rod Kimble
  • 1,302
  • 3
  • 18
  • 44
  • Check this : http://stackoverflow.com/questions/11558574/a-qr-scanner-inside-of-a-fragment – Vinay Pandravada Oct 30 '15 at 11:27
  • It doesnt help. I think my code is correct because in my MainActivity it works. But maybe the code isnt correct arranged. Wrong position? Do the `onActivityResult` method has to be inside or outside the `onCreateView` and why? – Rod Kimble Nov 02 '15 at 10:17
  • 1
    onActivityResult in Fragment is the problem! http://stackoverflow.com/questions/6147884/onactivityresult-not-being-called-in-fragment – Viktor Yakunin Nov 02 '15 at 11:31
  • After hours of research I recognized this :D but now I have anotherProblem. How to get my scanned string back to my Fragment? – Rod Kimble Nov 02 '15 at 11:33
  • I changed my Question due to the expanded problem – Rod Kimble Nov 02 '15 at 11:43

0 Answers0