2

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);

            }
        }



    }
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • add this tag to your activity (the one you want it to be always on portrait mode) in the `AndroidManifest.xml` file `android:screenOrientation="portrait"` – user 007 Jun 13 '18 at 12:23
  • it dont work. it turns just for one second to landscape and than ist turns back to portrait – Hurricane08 Jun 13 '18 at 12:33
  • possible to duplicate of -https://stackoverflow.com/questions/14066038/why-does-an-image-captured-using-camera-intent-gets-rotated-on-some-devices-on-a – Adil Jun 13 '18 at 12:40
  • add this in AndroidManifest of your activity -android:configChanges="orientation|screenSize" – Adil Jun 13 '18 at 12:42
  • Most probably the reason for `ListView` data being empty after rotation is tightly-coupled data and view. Consider using an architecture to decouple data and view (eg. MVP, MVVM, etc.), or pass the data between config changes (eg. using `onSaveInstanceState`) – TheKalpit Jun 13 '18 at 12:46
  • @Hurricane08 maybe the transition between activities is causing this. Try the solution in the linked topic to disable the animation and see if it still happens. https://stackoverflow.com/questions/6972295/switching-activities-without-animation – user 007 Jun 13 '18 at 12:48

2 Answers2

1

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.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
0

Put one tag android:screenOrientation="portrait" in manifest.xml for the activity you want in a potrait mode

so your code will look like:

 <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait" />
Android Geek
  • 596
  • 8
  • 14