onConfigurationChanged not getting called
The problem was that if you use this method
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
to force the orientation of your Activity
to portrait mode, you're not candidate to receive orientation
changes.
So the solution is to not setRequestOrientation
to a particular mode. But instead use SCREEN_ORIENTATION_SENSOR.
Why isn't onConfigurationChanged being called?
Use this code to get the confi chage
override fun onConfigurationChanged(newConfig: Configuration?) {
super.onConfigurationChanged(newConfig)
if (newConfig != null) {
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
}
A couple of things to try:
android:configChanges="orientation|keyboardHidden|screenSize"
rather than android:configChanges="orientation"
Ensure that you are not calling setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
anywhere. This will cause onConfigurationChange() to not fire.
onConfigurationChanged not getting called even with manifest config changes - Why?
The onConfigurationChanged
method is called when you physically rotated an Android device. So, you need to rotate your device to see the method gets called.
Note: The onConfigurationChanged also gets called when you changed locale, etc. See here for more info.
Also, I would have these values for the android:configChanges
directive:
android:configChanges="orientation|screenSize|layoutDirection
Also, Issue 61671 might be related to the screen orientation problem described in this post. As of April 2014, this defect / bug has not been resolved by Google.
WORKAROUND
As stated in this SO link, screen orientation might work if the device type is set as a generic 7" tablet instead of Nexus 7.
onConfigurationChanged not called anymore
From Honeycomb
onwards you need also screenSize
:
android:configChanges="orientation|keyboardHidden|screenSize"
Android onConfigurationChanged not being called
A couple of things to try:
android:configChanges="orientation|keyboardHidden|screenSize"
rather than android:configChanges="orientation"
Ensure that you are not calling setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
anywhere. This will cause onConfigurationChange() to not fire.
Check that you are not using android:screenOrientation
in your manifest.
If none of that works, read through the Android doc on handling runtime changes and make sure you are doing everything correctly. There may be something somewhere else in your code that's causing the problem. http://developer.android.com/guide/topics/resources/runtime-changes.html
EDIT: As derrik pointed out, I assumed that you were changing the configuration with the accelerometer detecting what way the device was facing. If you want the configuration to change as the keyboard is shown/hidden the configChanges in the manifest must include keyboardHidden
as well.
onConfigurationChanged is not getting triggered
The reason for the rotation not being detected was because I had set the device rotation to portrait (canceled autorotate) on my mobile phone. This prevented my app from rotating. Found the answer here:
onConfigurationChanged not getting called
The way to override this preference set by the user is to add the following code to the manifest as answered here: onConfigurationChanged is not getting called any time in android
android:screenOrientation="sensor"
Now the app rotates based on the sensor input and onConfigurationChanged is triggered. I hope this answer helps others get rid of this potential bug.
Android onConfigurationChanged() is not being called in Activity
The only thing that worked was using getLastNonConfigurationInstance(). http://developer.android.com/reference/android/app/Activity.html#getLastNonConfigurationInstance()
onConfigurationChanged not called once setRequestedConfiguration has been used
This is how I solved it. I am aware it is reinventing the wheel but it meets my requirement and I did not find a proper way to handle this with standard sdk tools.
First, create an OrientationManager
class that listen to orientation changes
public class OrientationManager extends OrientationEventListener{
private static final String TAG = OrientationManager.class.getName();
private int previousAngle;
private int previousOrientation;
private Context context;
private OrientationChangeListener orientationChangeListener;
private static OrientationManager instance;
private OrientationManager(Context context) {
super(context);
this.context = context;
}
public static OrientationManager getInstance(Context context){
if (instance == null){
instance = new OrientationManager(context);
}
return instance;
}
public int getOrientation(){
return previousOrientation;
}
public void setOrientation(int orientation){
this.previousOrientation = orientation;
}
@Override
public void onOrientationChanged(int orientation) {
if (orientation == -1)
return;
if(previousOrientation == 0){
previousOrientation = context.getResources().getConfiguration().orientation;
if (orientationChangeListener != null){
orientationChangeListener.onOrientationChanged(previousOrientation);
}
}
if (previousOrientation == Configuration.ORIENTATION_LANDSCAPE &&
((previousAngle > 10 && orientation <= 10) ||
(previousAngle < 350 && previousAngle > 270 && orientation >= 350)) ){
if (orientationChangeListener != null){
orientationChangeListener.onOrientationChanged(Configuration.ORIENTATION_PORTRAIT);
}
previousOrientation = Configuration.ORIENTATION_PORTRAIT;
}
if (previousOrientation == Configuration.ORIENTATION_PORTRAIT &&
((previousAngle <90 && orientation >= 90 && orientation <270) ||
(previousAngle > 280 && orientation <= 280 && orientation > 180)) ){
if (orientationChangeListener != null){
orientationChangeListener.onOrientationChanged(Configuration.ORIENTATION_LANDSCAPE);
}
previousOrientation = Configuration.ORIENTATION_LANDSCAPE;
}
previousAngle = orientation;
}
public void setOrientationChangedListener(OrientationChangeListener l){
this.orientationChangeListener = l;
}
public interface OrientationChangeListener{
public void onOrientationChanged(int newOrientation);
}
}
Then in your activity implement OrientationChangeListener
and override onOrientationChanged()
:
public class MyActivity extends Activity implements OrientationChangeListener{
private OrientationManager orientationManager;
@Override
public void onCreate(Bundle b){
orientationManager = OrientationManager.getInstance(this);
orientationManager.setOrientationChangedListener(this);
}
@Override
public void onOrientationChanged(int newOrientation) {
orientation = newOrientation;
if (newOrientation == Configuration.ORIENTATION_LANDSCAPE){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
setLandscapeConfig();
}else{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setPortraitConfig();
}
}
So I don't use onConfigurationChanged
anymore but keep the following line in the Manifest:
android:configChanges="orientation|screenSize"
Related Topics
How to Change Android Minsdkversion in Flutter Project
Viewpager as a Circular Queue/Wrapping
How to Resize a Custom View Programmatically
Android Spinner with Multiple Choice
Android: Getting a File Uri from a Content Uri
Enable/Disable Data Connection in Android Programmatically
Data Binding Class Not Generated
How to Implement Recyclerview with Cardview Rows in a Fragment with Tablayout
Android Service Stops When App Is Closed
Webview: How to Avoid Security Alert from Google Play Upon Implementation of Onreceivedsslerror
Convert a PDF Page into Bitmap in Android Java
Android P - 'Sqlite: No Such Table Error' After Copying Database from Assets
Media Player Called in State 0, Error (-38,0)
Use Picasso to Get a Callback with a Bitmap
Passing Data Between Fragments to Activity