Search code examples
androidandroid-intentbarcode-scannerandroid-sparsearray

Passing the result from SparseArray to another activity


I am trying to scan a QR code and pass the value from the scanning result to another activity. I get the result in a SparseArray and extract the most latest scanned value. I am unable to retrieve any string in my second activity. Can anyone tel me if the results in SparseArray are of the string format? If not, how can I receive these values in my second activity in string format?

My MainActivity

@Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
    final SparseArray<Barcode> barcodes = detections.getDetectedItems();
    if (barcodes.size() != 0) {
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        intent.putExtra("barcode",barcodes.valueAt(0));
        startActivity(intent);
        finish();
    }
}

My Receiving Activity

Intent intent = getIntent();
String barcode = intent.getStringExtra("barcode");

Solution

  • barcodes.valueAt(0) is returning a Barcode which is implementing Parcelable. In your receiving Activity you should do:

    Barcode barcode = (Barcode) intent.getParcelableExtra("barcode");