Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IllegalStateException: Only fullscreen opaque activities can request orientation [duplicate]

I am facing the problem while retrieving the contacts from the contact book in Android 8.0 Oreo java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation

I am trying to get the contact in my activity from the phone contact book and it works perfect for Lollipop, Marshmallow, Nougat, etc but it will gives me the error for Oreo like this please help me. My code is here below.

Demo Code :-

private void loadContacts() {
            contactAsync = new ContactLoaderAsync();
            contactAsync.execute();
        }

        private class ContactLoaderAsync extends AsyncTask<Void, Void, Void> {
            private Cursor numCursor;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                Uri numContacts = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
                String[] numProjection = new String[]{ContactsContract.CommonDataKinds.Phone.CONTACT_ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE};
                if (android.os.Build.VERSION.SDK_INT < 11) {
                    numCursor = InviteByContactActivity.this.managedQuery(numContacts, numProjection, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE NOCASE ASC");
                } else {
                    CursorLoader cursorLoader = new CursorLoader(InviteByContactActivity.this, numContacts, numProjection, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE NOCASE ASC");
                    numCursor = cursorLoader.loadInBackground();
                }

            }

            @Override
            protected Void doInBackground(Void... params) {
                if (numCursor.moveToFirst()) {
                    try {
                        final int contactIdIndex = numCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);
                        final int displayNameIndex = numCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                        final int numberIndex = numCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                        final int typeIndex = numCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
                        String displayName, number, type;
                        do {
                            displayName = numCursor.getString(displayNameIndex);
                            number = numCursor.getString(numberIndex);
                            type = getContactTypeString(numCursor.getString(typeIndex), true);
                            final ContactModel contact = new ContactModel(displayName, type, number);
                            phoneNumber = number.replaceAll(" ", "").replaceAll("\\(", "").replaceAll("\\)", "").replaceAll("-", "");

                            if (phoneNumber != null || displayName != null) {
                                contacts.add(phoneNumber);
                                contactsName.add(displayName);
                                contactsChecked.add(false);

                                filterdNames.add(phoneNumber);
                                filterdContactNames.add(displayName);
                                filterdCheckedNames.add(false);
                            }
                        } while (numCursor.moveToNext());
                    } finally {
                        numCursor.close();
                    }
                }


                Collections.sort(contacts, new Comparator<String>() {
                    @Override
                    public int compare(String lhs, String rhs) {
                        return lhs.compareToIgnoreCase(rhs);
                    }
                });

                InviteByContactActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mContactAdapter.notifyDataSetChanged();
                    }
                });

                return null;
            }
        }

        private String getContactTypeString(String typeNum, boolean isPhone) {
            String type = PHONE_TYPES.get(typeNum);
            if (type == null)
                return "other";
            return type;
        }

        static HashMap<String, String> PHONE_TYPES = new HashMap<String, String>();

        static {
            PHONE_TYPES.put(ContactsContract.CommonDataKinds.Phone.TYPE_HOME + "", "home");
            PHONE_TYPES.put(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE + "", "mobile");
            PHONE_TYPES.put(ContactsContract.CommonDataKinds.Phone.TYPE_WORK + "", "work");
        }
}

Error Log:-

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example, PID: 6573
                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.Activity.InviteByContactActivity}: java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
                                                              Caused by: java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
like image 820
Shubham Sejpal Avatar asked Jan 03 '18 06:01

Shubham Sejpal


People also ask

How to enable screen orientation in activity tag?

There is no screenOrientation attribute in the activity tag, and the only thing that causes the crash is screenOrientation being set while android:windowIsTranslucent is set to true. This does not work, unless comments above is used. See answer by Radesh, which covers comments above, and more, and is clear

How to add orientation lock to an activity programmatically?

But if you need it, you can add it programmatically before 'super.onCreate': setTheme (R.style.Theme_AppCompat_Light_Dialog); If you use a fullscreen transparent activity, there is no need to specify the orientation lock on the activity. It will take the configuration settings of the parent activity. So if the parent activity has in the manifest:

When does screen_orientation_landscape trigger an exception?

According to the above code analysis, when TargetSdkVersion>=27, when using SCREEN_ORIENTATION_LANDSCAPE, SCREEN_ORIENTATION_PORTRAIT, and other related attributes, the use of windowIsTranslucent, windowIsFloating, and windowSwipeToDismiss topic attributes will trigger an exception.

Why is my activity not showing up as full screen?

Probably you showing Activity looking like Dialog (non-fullscreen), so remove screenOrientation from Manifest or from code. This will fix the issue. Hmm very strange, for me that was the case. Try running the app on different Android versions, see what will be the result this may help you...


3 Answers

In android Oreo (API 26) you can not change orientation for Activity that have below line(s) in style

 <item name="android:windowIsTranslucent">true</item>

or

 <item name="android:windowIsFloating">true</item>

You have several way to solving this :

1) You can simply remove above line(s) (or turn it to false) and your app works fine.

2) Or you can first remove below line from manifest for that activity

android:screenOrientation="portrait"

Then you must add this line to your activity (in onCreate())

'>=' change to '!=' thanks to Entreco comment

    //android O fix bug orientation
    if (android.os.Build.VERSION.SDK_INT != Build.VERSION_CODES.O) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

3) You can create new styles.xml in values-v26 folder and add this to your style.xml. (Thanks to AbdelHady comment)

 <item name="android:windowIsTranslucent">false</item>
 <item name="android:windowIsFloating">false</item>
like image 128
Radesh Avatar answered Oct 05 '22 22:10

Radesh


some of the answers wasnt clear for me and didnt work,

so this was causing the error:

<activity
android:name=".ForgotPass_ChangePass"
android:screenOrientation="portrait" <--- // this caused the error
android:windowSoftInputMode="stateHidden|adjustPan|adjustResize"/>

android studio was suggeting to set screenOrientation to fullSensor

android:screenOrientation="fullSensor"

yes this will fix the error but i wan to keep my layout in the portrait mode, and fullSensor will act based on the sensor

"fullSensor" The orientation is determined by the device orientation sensor for any of the 4 orientations. This is similar to "sensor" except this allows any of the 4 possible screen orientations, regardless of what the device will normally do (for example, some devices won't normally use reverse portrait or reverse landscape, but this enables those). Added in API level 9.

source: android:screenOrientation

so the solution that worked for me i used "nosensor" :

 <activity
        android:name=".ForgotPass_ChangePass"
        android:screenOrientation="nosensor"
        android:windowSoftInputMode="stateHidden|adjustPan|adjustResize"/>

"nosensor" The orientation is determined without reference to a physical orientation sensor. The sensor is ignored, so the display will not rotate based on how the user moves the device.

see android documentation here

like image 43
Ridha Rezzag Avatar answered Oct 06 '22 00:10

Ridha Rezzag


Just Set Orientation of activity in Manifiest.xml

android:screenOrientation="unspecified"

OR for restricted to Portrait Orientation

You can also use in Activity, In onCreate method call before super.onCreate(...) e.g.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        setOrientation(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_xml_layout);
        //...
        //...
}

// Method 
public static void setOrientation(Activity context) {
   if (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.O)
        context.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
      else
        context.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
like image 25
Attaullah Avatar answered Oct 06 '22 00:10

Attaullah