Android: screen goes back to portrait mode when turning screen off - android

In my manifest file I specified android:configChanges="orientation". And I noticed that when turning off the screen while in landscape the onConfigurationChanged() method is called once with portrait status and then called again with landscape status when waking up the screen.
What is the reason for this ? Is there any way to disable it ?

Use this in your manifest file--
<android:configChanges="orientation|keyboardHidden" >
This will never let your screen change when you rotate the screen landscape or portrait.

Use this one in your android menifest
android:configChanges="orientation"
for landscap
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
if you want unspecified
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

The problem seems related to the lock screen which forces the layout back to portrait when switching screen off/on.
I solved my issue by ignoring calls to onConfigurationChanged() between the calls to onPause() and onResume()

Related

Oreo (8.1.0): Portrait orientation issue

I'd like to have all activities in portrait orientation.
So I have tried both
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
in onCreate(), onPause() or onResume() but the screen will flip to landscape and back to portrait in case of I rotate device in landscape from the beginning before opening the app.
But if I remove setRequestedOrientation method and just add android:screenOrientation="portrait" in AndroidManifest.xml only then it works (but let's imagine if I have 30 activities so it's better to have in BaseActivity instead)
How to reproduce
BaseActivity has method in onCreate(), onResume(), onPause() setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Rotate device in landscape orientation.
Open app, Activity A is flipping from landscape to portrait.
Start Activity B from Activity A, Activity B is flipping from landscape to portrait.
Note
I have checked already
Android 8.1 screen orientation issue: flipping to landscape a portrait screen
Activity rotating itself and back to normal in android 8.1
but it didn't work (it works only if orientation is defined in AndroidManifest)
If someone knows how to fix please share the solution
Thank you so much.

How to force activity always appear in portrait orientation without delay in lockscreen?

I have an activity with "portrait" orientation in AndroidManifest.xml.
When I start it from lock screen in landscape orientation, it appear with landscape for 1 second, and then change to portrait. The delay happens even if it's an blank activity (no view, no task).
Tell me how to avoid this delay. Remove transition effect, rotation effect? I know it could be device performance, but still want to fix this.
If you always wants to run your app in landscape mode than use this code in Manifest file for all activity
<activity
android:name=".yourAcitvity"
android:launchMode="singleTask"
android:screenOrientation="landscape"
android:configChanges="orientation|screenSize"
/>
That't the default behavior of Android, you can verify this issue using system apps. For ex: open the Android's default messenger, and turn it to landscape and lock the phone, now unlock it and you can see the messenger has re aligned to portrait, that's because the home screen is set to portrait by default. In order to show the home screen the system aligns the device to portrait again.

How do i prevent an orientation change of a background activty?

I'm preventing an activty from changing to landscape mode via
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
everything works fine except if i call a new activity. When the new activity turns to landscape mode the background activity changes too. How do i prevent this?
Fix your activity to portrait mode when in onPause.
onPause(){
// Your code to make this app fixed to portrait when in background
}

How to lock screen orientation when anctivity isn't recreated

I have an activity used to display a video (VideoView).
By default the user can change screen orientation to see the video.
I avoid destroying/recreating the activity upon orientation changes because of latency issue.
The rotation is painless without any sound break.
android:configChanges="keyboardHidden|orientation|screenSize"
What I want to do now is add a button that will force the activity to remain on current orientation.
The problem is that because the activity isn't recreated after the rotation, I don't know which method to override to avoid the rotation to happen.
Overriding onConfigurationChanged is useless because the rotation already happened.
Use setRequestedOrientation().
E.g. to lock the orientation to portrait, call:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
In your activity.

android set app to portrait only, except when keyboard pulled out

a while back I converted my app to include landscape mode, from each activity having screenOrientation="portrait" to this:
<activity
android:name="bundle.android.views.MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="fullSensor">
I also handle configuration changes just fine in onConfigurationChanged in each activity.
But in hindsight, I only want edge cases with pop out hardware keyboards to get landscape mode. How do I adjust my manifest and code?
do I keep a certain combination android:configChanges ? the onConfigurationChanged class? insight appreciated
One option is to tackle this problem programmatically. What you could do is detect if the user has a hardware keyboard attached, and if so force the orientation into landscape mode. Here are two ways to detect a hardware keyboard:
http://developer.android.com/reference/android/content/res/Configuration.html#keyboard
and
if (getResources().getConfiguration().hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
}
Once you have detected that the user has a hard keyboard you can force landscape mode on the user with:
myActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Categories

Resources