Hurricane08
Hurricane08

Reputation: 27

Activity turns to landscape after opening the camera

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

            }
        }



    }

Upvotes: 2

Views: 196

Answers (2)

Alex Cohn
Alex Cohn

Reputation: 57203

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.

Upvotes: 1

Android Geek
Android Geek

Reputation: 636

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" />

Upvotes: 0

Related Questions