android: don't keep adding BarcodeScannerView instances to contentFrame when starting camera
When putting app to background with qr scanner active, then moved to foreground again, SimpleScannerActivity instance is not destroyed, but the local mScannerView was re-initialized with a new instance (through startCamera()) regardless of pre-existing instance, and added to the contentFrame. This leaves the previous mScannerView instance in limbo, potentially getting garbage collected at unpredictable times.
This commit is contained in:
@@ -88,6 +88,7 @@ public class SimpleScannerActivity extends Activity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void startCamera() {
|
private void startCamera() {
|
||||||
|
if (mScannerView == null) {
|
||||||
mScannerView = new BarcodeScannerView(this);
|
mScannerView = new BarcodeScannerView(this);
|
||||||
mScannerView.setCropRatio(0.75f); // Set crop ratio to 75% (this defines the square area shown in the scanner view)
|
mScannerView.setCropRatio(0.75f); // Set crop ratio to 75% (this defines the square area shown in the scanner view)
|
||||||
// by default only Format.QR_CODE is set
|
// by default only Format.QR_CODE is set
|
||||||
@@ -99,6 +100,7 @@ public class SimpleScannerActivity extends Activity {
|
|||||||
// Return false to stop scanning after first result
|
// Return false to stop scanning after first result
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
}
|
||||||
mScannerView.openAsync(); // Start camera on resume
|
mScannerView.openAsync(); // Start camera on resume
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user