When I open the camera my Activity turns for a second to landscape and than back to portrait after that all data in my ListView are gone. Do you know how to fix this?
I tried to use SCREEN_ORIENTATION_PORTRAIT but it didn't worked for me.
Here is my code.
private void onClick() {
scanBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
IntentIntegrator integrator = new IntentIntegrator(MainActivity.this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Scan Code");
integrator.setBeepEnabled(false);
integrator.setCameraId(0);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
scandata = result.getContents();
scanFormat = result.getFormatName();
if (scandata != null) {
dta.add("Data: " + scandata + " Format: " + scanFormat);
}
}
}
Your device does not behave well, but you should not despair. If you add a "proxy" activity which will hide the list and be responsible for launching the camera intent and processing the result, your main activity will be protected from unwanted rotation.
On the other hand, empty list view may still reappear, if not on your device, then on a device of your customer, especially on devices with small RAM or bad camera app.
You should save and restore your activity state when necessary, as explained in this classic answer.