Android: Auto Rotate not working after setRequestedOrientation - android

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!

Related

1 Activity, 2 Fragments and setting orientation

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.

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.

Auto screen orientation is not working one user ser screen orientation android

I have create a custom media player.In which there is a button for landscape and portrait mode. When player opens media player,its all feature works fine.But on click fullscreen button, i set media player to landscape mode. After this button click auto screen orientation is not working, only user can change orientation by clicking button(button to change landscape/portrait). Is there any method to work both auto screen orientation with button(button to change landscape/portrait)?
Thanks.
In your manifest file, you have given permisssion for potrait and landscape, So you have avoid that, instead of that do it in your activity.

onConfigurationChanged not getting called the first time

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.

How to keep android search dialog on when rotating screen?

I built a search widget based on the code, Searchable Dictionary v2. Everything is working fine, but there is one problem.
If I rotate the screen from portrait to landscape or vice versa when the search dialog box is on, the text edit box and keyboard screen get disappeared and runs again.
Is there any way that I can make that it switches the edit box into appropriated mode(landscape or portrait) without exiting and re-running the dialog box?
You can check the behavior with the searchable dictionary.
I want something like the google search widget. It just switches edit box mode while it keeps the screen.
Once the screen orientation is changed, Android creates a new activity/view, so you'll may have to handle screen orientation by yourself. I used this solution in an app successfully, I assume it also applies to widgets.
See this page on how to do that:
http://www.androidpeople.com/android-how-to-handle-screen-orientation-change-issue/
Some other helpful hints (basically the same solutions) are here:
How do I handle screen orientation changes with an activity started within a tab's activity
How to handle screen orientation change when progress dialog and background thread active?
Some information concerning onPause() and onSaveInstanceState() (which may not apply as you're using a widget, but just in case :) ) is here:
How do I disable orientation change on Android?
What i did was to lock the orientation while the dialog is displayed, then unlock it when finished:
int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); //locks landscape
}
else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); //locks port
}
// do work
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

Categories

Resources