Search code examples
javaandroidandroid-studiobluetoothzxing

How to separate activities


I have several onActivityResult activities in my Android app and I am having an issue with two activities running at one time. When I open up my ZXing barcode scanner it will close the connection to my Bluetooth printer. Here is my code:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REQUEST_ENABLE_BT:
                if (resultCode == Activity.RESULT_OK) {
                    Toast.makeText(this, "Bluetooth open successful", Toast.LENGTH_LONG).show();
                } else {
                    finish();
                }
                break;
            case REQUEST_CONNECT_DEVICE:
                if (resultCode == Activity.RESULT_OK) {
                    String address = data.getExtras()
                            .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
                    con_dev = mService.getDevByMac(address);

                    mService.connect(con_dev);
                }
                break;
        }
            IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
            if (scanningResult != null) {
                String scanContent = scanningResult.getContents();
                String scanFormat = scanningResult.getFormatName();
                //formatTxt.setText("FORMAT: " + scanFormat);
                edtContext.setText(scanContent);
            } else {
                Toast toast = Toast.makeText(getApplicationContext(),
                        "No scan data received!", Toast.LENGTH_SHORT);
                toast.show();
            }
    }

What would be the best way to split up these activities so that they do not overlap each other?

UPDATE: As @Mahfa stated I need to add a requestCode to the scanning activity. This is my code for the button that starts the scanning activity:

btnBarcode = (Button) findViewById(R.id.btnBarcode);
        OnClickListener BarcodeOnClickListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getId() == R.id.btnBarcode) {
                    IntentIntegrator integrator = new IntentIntegrator(PrintDemo.this);
                    integrator.initiateScan();
                }
            }
        };
        btnBarcode.setOnClickListener(BarcodeOnClickListener);

What do I need to do to add a requestCode to this?


Solution

  • @Mahfa put me on the right track with the links that he posted and eventually I found this code example that cleared up my issue http://www.programcreek.com/java-api-examples/index.php?source_dir=jdkernel-updater-master/src/jdkernel/ui/ThemeListNewActivity.java

    Here is my completed code:

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
            switch (requestCode) {
                case REQUEST_ENABLE_BT:
                    if (resultCode == Activity.RESULT_OK) {
                        Toast.makeText(this, "Bluetooth open successful", Toast.LENGTH_LONG).show();
                    } else {
                        finish();
                    }
                    break;
                case REQUEST_CONNECT_DEVICE:
                    if (resultCode == Activity.RESULT_OK) {
                        String address = data.getExtras()
                                .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
                        con_dev = mService.getDevByMac(address);
    
                        mService.connect(con_dev);
                    }
                    break;
                case IntentIntegrator.REQUEST_CODE:
                    IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
                    if (scanningResult != null) {
                        String scanContent = scanningResult.getContents();
                        String scanFormat = scanningResult.getFormatName();
                        //formatTxt.setText("FORMAT: " + scanFormat);
                        edtContext.setText(scanContent);
                    } else {
                        Toast toast = Toast.makeText(getApplicationContext(),
                                "No scan data received!", Toast.LENGTH_SHORT);
                        toast.show();
                    }
                    break;
            }
    

    I did not have to change anything on my OnClickListener. Thanks for pointing me in the right direction @Mahfa.