Search code examples
javaandroidzxingbarcode-scanner

2 Scanners but one onActivityResult?


I am working on a mobile application in Android Studio and have 2 buttons. Each button will initiate a barcode/QR scan using the Zxing library.

The problem that I am having is that I am assigning the results from the first scan to the button text and that is working fine.

However, when it comes to assigning the results from the second scan to its button then it does not work as there is only one onActivityResult?

I am aware that the system will just ignore it if I copy and paste the code and try to Override an onActivityResult2 as it is not recognised by the system.

However, how am I meant to get around this problem?

Here is my code for the onActivityResult and also onActivityResult2 just to show what I am trying to achieve:

onActivityResult:

WORKING CODE:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);

    if (result != null) {
        //if qrcode has nothing in it
        if (result.getContents() == null) {
            Toast.makeText(this, "Result Not Found", Toast.LENGTH_LONG).show();
        } else {
            //if qr contains data
            try {
                //converting the data to json
                JSONObject obj = new JSONObject(result.getContents());

            } catch (JSONException e) {
                e.printStackTrace();
                if (buttonId == R.id.buttonScan) {
                    machineType = result.getContents();
                    scanBarcode.setText("Machine Type: " + machineType);
                } else if (buttonId == R.id.buttonScan2) {
                    workOrderNumber = result.getContents();
                    scanWorkOrder.setText("Work Order Number: " + workOrderNumber);
                }
                else if (buttonId == 0) {
                    ???
                }

            }
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

Buttons to invoke library:

private IntentIntegrator qrScan;
private IntentIntegrator qrScan2;

int buttonId = 0; //THIS IS GLOBAL

 //Initialise Buttons
    scanBarcode = findViewById(R.id.buttonScan);
    scanWorkOrder = findViewById(R.id.buttonScan2);
    submit = findViewById(R.id.buttonSubmit);

    sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

    //Initialize Scanners
    qrScan = new IntentIntegrator(this);


    scanBarcode.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            qrScan.initiateScan();
            buttonId = R.id.buttonScan;
        }
    });

    scanWorkOrder.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            qrScan.initiateScan();
            buttonId = R.id.buttonScan2;

        }
    });

Solution

  • int buttonId = 0;
    

    Add as global variable.

    buttonId = R.id.buttonScan;
    

    Add to onClick of first button.

    buttonId = R.id.buttonScan2;
    

    Add to onClick of second button.

    Check what you get in onActivityResult and handle accordingly.