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.
Related
I have an Activity where the 1st Fragment (whose orientation is locked) solicits input from a user. This data is used to determine how to orient the 2nd Fragment. This orientation will also be locked.
I am guessing I will have to make a call to setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) either from the Activity or the 2nd Fragment.
My question is where best to do this. Does it really make a difference? Can this action be performed w/o undergoing an Activity restart?
If changing the setRequestedOrientation state requires the screen to rotate then it will restart the activity.
For example if you request ActivityInfo.SCREEN_ORIENTATION_PORTRAIT while the screen is in landscape, it will restart.
However it will not restart if the phone is already in that orientation.
I have a video player in which I have two features:
If the auto-rotate is disabled and when the user clicks on fullscreen button I force the orientation to go in landscape view using setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE) and back to portrait view when fullscreen is exited.
I have overridden the onConfigurationChanged to detect the change in orientation when the auto-rotate feature is enabled by user and change the activity view according to the orientation.
Now the problem with this setup is that if user start the activity and goes in fullscreen and setRequestedOrientation get invoked once, later when auto-rotate is enabled, the activity doesn't receive the orientation changes and the onConfigurationChanged doesn't get called.
I read here (https://stackoverflow.com/a/6109206/5167868) that they wont work simultaneously.
I would like to know if there a way of implementing both as I have seen players like MX Player do this in their app.
After you fix the orientation of the screen by using setRequestedOrientation(), Auto Rotate feature won't work. So to make it work, you may do something like this:
After you set your orientation using: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE)
Call this:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
This shall make the auto rotate to work again!
When the screen orientation changed between (Reverse) Landscape and (Reverse) Portrait, the onCreate method of Activity would always be called. But when the screen orientation changed only between Landscape and Reverse Landscape or between Portrait and Reverse Portrait, the onCreate method even onResume method of Activity would not be called!
How to detect the event about reversing screen orientation?
If I use
android:configChanges="orientation|screenSize"
and
public void onConfigurationChanged(Configuration newConfig)
No mater how I change the screen orientation, the onCreate method and onResume method of Activity would not be called, so that the views' positions on Activity are very weird. So, I prefer the solution about "how to detect the reversing screen orientation event" not to use configChanges.
Does anyone know how to solve it?
You can try this method screenOrientationDidReverse:. when device only reverse the orientation, this method will be called. so... override the method!
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.