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
After doing some research, it seems that this problem may be due to a google bug. For me, I was able to leave this line in my Activities onCreate method:
AND I changed my targetSdkVersion to 26. Having that line in my onCreate still resulted in a crash while my targetSdkVersion was still set at 27. Since no one else's solution has worked for me thus far, I found that this works as a temporary fix for now.
only 8.0.0 throw the exception, above 8.0 has remove the exception
In android Oreo (API 26) you can not change orientation for Activity that have below line in style
You have several way to solving this :
1) You can simply remove above line (or turn it to false) and your app works fine.
2) Or you can first remove below line from manifest for that activity
Then you must add this line to your activity (in onCreate())
3) You can create new
styles.xml
invalues-v26
folder and add this to yourstyle.xml
. (Thanks to AbdelHady comment)Probably you showing Activity looking like Dialog(non-fullscreen), so remove screenOrientation from Manifest or from code. This will fix the issue.
It is a conflict (bug) between Themes inside style.xml file in android versions 7 (Api levels 24,25) & 8 (api levels 26,27), when you have
android:screenOrientation="portrait"
:inside specific activity (that crashes) in AndroidManifest.xml&
in the theme that applied to that activity inside style.xml
It can be solve by these ways according to your need :
1- Remove on of the above mentioned properties that make conflict
2- Change Activity orientation to one of these values as you need :
unspecified
orbehind
and so on that can be found here : Google reference for android:screenOrientation `3- Set the orientation programmatically in run time
I faced this problem only in SDK 26 (8.0.0) if using windowIsTranslucent 'true' and forcefully setting orientation:
Here's the solution: