I tried many approaches after reading multiple pages on stackoverflow but I can't seem to get it working.
I have a cardview with onclicklistener to each child clicked (using setSingleEvent
).
One of the child uses a barcode scanner then passes the scanned barcode to another activity (via onActivityResult
).
But now I need another child (cardview) to use the same barcode scanner and pass the value to another activity as well. I don't know how to differentiate both clicks in the onActivityResult
method.
However, the code works fine for a single child grid implementing the barcode scanner (finali == 0)
. But, in case (finali == 3)
, how do I pass to the onActivityResult
a different thing to do?
private void setSingleEvent(GridLayout mainGrid) {
//Loop all child item of main Grid
for (int i = 0;i<mainGrid.getChildCount();i++){
CardView cardView = (CardView) mainGrid.getChildAt(i);
final int finali=i;
cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (finali == 0){
Intent intent = BarcodeReaderActivity.getLaunchIntent(getApplicationContext(), true,false);
startActivityForResult(intent, BARCODE_READER_ACTIVITY_REQUEST);
}
else if (finali == 1){
startActivity(new Intent(getApplicationContext(),AllActiveCheckInActivity.class));
}
else if (finali == 2){
Log.d(TAG, "onClick: Reports Clicked");
}
else if (finali == 3){
Intent intent = BarcodeReaderActivity.getLaunchIntent(getApplicationContext(), true,false);
startActivityForResult(intent,BARCODE_READER_ACTIVITY_REQUEST);
}
}
});
}
}
Here is my onActivityResult
that works fine when I make use of only one of the child cardview to implement the barcode scanner.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK) {
Toast.makeText(this, "error in scanning", Toast.LENGTH_SHORT).show();
return;
}
if (requestCode == BARCODE_READER_ACTIVITY_REQUEST && data != null) {
Barcode barcode = data.getParcelableExtra(BarcodeReaderActivity.KEY_CAPTURED_BARCODE);
String terminal_id = barcode.rawValue;
Intent intent1 = new Intent(this,LocationDetails.class);
intent1.putExtra("terminal_id",terminal_id);
startActivity(intent1);
}
}
Pass a different constant as requestCode when calling startActivityForResult
instead of passing the same one (BARCODE_READER_ACTIVITY_REQUEST
) to each call:
if (finali == 0) {
Intent intent = BarcodeReaderActivity.getLaunchIntent(getApplicationContext(), true,false);
startActivityForResult(intent, BARCODE_READER_ACTIVITY_REQUEST_FINALI_1);
}
// ... the remaining code of finali == 1 & finali == 2
else if (finali == 3) {
Intent intent = BarcodeReaderActivity.getLaunchIntent(getApplicationContext(), true,false);
startActivityForResult(intent,BARCODE_READER_ACTIVITY_REQUEST_FINALI_3);
}
And then check the requestCode in the onActivityResult method:
if (requestCode == BARCODE_READER_ACTIVITY_REQUEST_FINALI_1 && data != null) {
// Code for finali == 1
} else if (requestCode == BARCODE_READER_ACTIVITY_REQUEST_FINALI_3 && data != null) {
// Code for finali == 3
}