i am programming an App with some Android Scanners to scan a few barcodes and put them into a list. If you scan a code, which is already in the list, a toast shall appear on the screen. The problem is it doesn't.
This is the method which is called after a scan (it is located in the MainActivity):
public void onData(ScanDataCollection scanDataCollection) {
// The ScanDataCollection object gives scanning result and the collection of ScanData
// Check the data and its status.
String dataStr = "";
if ((scanDataCollection != null) && (scanDataCollection.getResult() == ScannerResults.SUCCESS)) {
ArrayList<ScanDataCollection.ScanData> scanData = scanDataCollection.getScanData();
for (ScanDataCollection.ScanData data : scanData) {
// Get the scanned dataString barcodeData
String barcodeData = data.getData();
// Get the type of label being scanned
ScanDataCollection.LabelType labelType = data.getLabelType();
dataStr = barcodeData;
}
// Result of scanned data
Log.i("SCANNER", "Scanned barcode " + dataStr);
for (Container c : containerList) {
if (c.getId().equals(dataStr)) {
//Toast.makeText(this, "Container bereits vorhanden!", Toast.LENGTH_LONG).show();
showToast("Container bereits vorhanden!");
return;
}
}
addNewContainer(dataStr);
}
}
The called showToast() method:
public void showToast(String toast) {
if (fragManager.findFragmentById(R.id.fragment_holder).getTag().equals("LIST_FRAGMENT")) {
((ListFragment) fragManager.findFragmentById(R.id.fragment_holder)).showToast(toast);
}
}
And the showToast() method of the fragment:
public void showToast(String toast) {
Log.i("FRAGMENT", "Before Toast");
Toast.makeText(ctx, "Schon vorhanden!", Toast.LENGTH_LONG).show();
Log.i("FRAGMENT", "After Toast");
}
The code does reach the toast, but it doesn't get any further. Log looks like this:
2020-12-03 16:49:00.422 6915-7014/com.fmg.containerapp I/SCANNER: Scanning...
2020-12-03 16:49:00.492 6915-7015/com.fmg.containerapp I/SCANNER: Scanned barcode AYYC411FX
2020-12-03 16:49:00.492 6915-7015/com.fmg.containerapp I/FRAGMENT: Before Toast
2020-12-03 16:49:00.507 6915-7016/com.fmg.containerapp I/SCANNER: Scanner is enabled and idle...
2020-12-03 16:49:00.552 6915-7017/com.fmg.containerapp I/SCANNER: Waiting for scan...
What did i do wrong, that the toast doesn't show up? I tried different contexts and it does work in the onCreateView() of every Fragment.
Thanks for your help