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.
Related
I want my view to launch in landscape mode. But once the user changes to landscape orientation and come back to portrait, I want my view to behave normally and change orientations according to device orientation.
Basically. view should launch in landscape only for the first time and behave normally after that.
Write at onStart of your Activity
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
setRequestedOrientation(YourActivity.SCREEN_ORIENTATION_LANDSCAPE);
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
setRequestedOrientation(YourActivity.SCREEN_ORIENTATION_PORTRAIT);
I have an activity that is locked in landscape from manifest. One of my fragments requires Portrait mode. Before I switch to that fragment I call setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); on my activity.
This works fine, however if I attempt to setRequestedOrientation from onResume (normally if the Portrait fragment was the last one opened by the user and the activity got restarted), I still get my activity drawn in landscape.
Bug or I'm missing some lifecycle trick here?
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()
I've a problem trying to capture the onConfigurationChanged event. This is the scenario:
Activity A starts (listens to onConfigurationChanged)
Phone rotated to landscape mode (onConfigurationChanged being called). Start activity B.
Activity B starts (listens to onConfigurationChanged) (LANDSCAPE)
Activity B rotates back to portrait (onBackPressed event raised). Activity B is destroyed and A is called back.
Activity A resumes
Phone rotated to landscape mode. The onConfigurationChanged is not called this time.
Phone rotated to portrait mode. The onConfigurationChanged called.
Phone rotated to landscape mode. The onConfigurationChanged called.
Why step 6 don't call onConfigurationChanged event? it doesn't make sense at all. Do you know what could be the issue?
I have the same problem, I update UI onResume() to fix it.
I had exactly the same issue. I still don't understand this behavior of android system, but you can use onOrientationChanged of OrientationEventListener instead of configuration change handling.
See this answer (example is not perfect but shows the way): https://stackoverflow.com/a/13844242/554281
I have faced same problem and was stuck in this for more than a week. Then I have prepared a sample APP with tabHost and reproduced the same issue on the sample app. After playing around with that, I found that this is a bug of android's tabHost. So, I have migrated tabHost to FragmentTabHost and found the problem is gone.
The simulation of the Issue with tabHost:
Let, there are two tab i.e A and B
Simulation 1 (Arise the bug)
Arrive on A in portrait mode
Go to B on portrait mode
Rotate B to landscape
Back to A on landscape mode.
Rotate A to portrait (onConfigChanged method of tabActivity gets fired. But onConfigChanged of A activity doesn't get fired)
Simulation 2 (Works fine)
Arrive on A in portrait Mode
Go to B on portrait mode (continue rotating as you want, but stop on portrait mode)
Back to A on portrait mode.
Rotate A to landscape (both onConfigurationChanged method of tabActivity and A activity gets fired)
Summary: If you get back to the screen in the same orientation you got out, both onConfigurationChanged() will get called.
I use in the AndroidManifest:
<activity android:name="Zoom" android:text="#string/Name" android:screenOrientation="landscape"></activity>
to force this specific Activity(B) to be been see as landscape, my problem is when i am moving to the next Activity(c) or back to the Activity(A) its still show as landscape
what i should do different??
thanks for helping!!
If your device is in landscape orientation, and you're not specifically setting orientation for the other activities, then they will show in landscape orientation.
Force the other activities to portrait with android:screenOrientation="portrait".
This will mean if your device is in landscape and you switch activity, the new activity will display in portrait (on its side) and the user will then have to rotate the phone into portrait mode to view it correctly