My app uses Barcode Scanner. I want to launch the scanner when I open the app so I have it in the onCreate method. The problem is that if I have it like that, when I turn the device it calls again onCreate and calls another scanner.
Also I have the first activity that calls the scanner. it has a menu so if the user presses back, it goes to that menu. If I turn the screen on that menu, it goes to barcode scanner again.
To solve it I have a flag that indicates if it is the first time I call the scanner, if it’s not I don’t call it again. Now the problem is that if I go out of the app and go in again it doesn’t go to the scanner, it goes to the menu, becasuse is not the first time I call it.
Any ideas? Is there a way to change the flag when I go out of my main activity or any other solution? My code.
private static boolean first = true;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
integrator = new IntentIntegrator(this);
if (first) {
first = false;
integrator.initiateScan();
}
}
In your Application manifest.xml, add this to your Activity where this barcode scanner is called
like this
what it does is, when the device is rotated, it will “do nothing” which mean it will not call the activity again (which eventually avoids calling onCreate())