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"


Calling setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); will reactivate onConfigurationChanged. You can set up a timer like this:

// Force orientation to portrait
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);

// Reactivate sensor orientation after delay
Timer mRestoreOrientation = new Timer();
mRestoreOrientation.schedule(new TimerTask() {
    @Override
    public void run() {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
    }
}, 2000);

We can only suppose the user will turn the device by himself to the forced orientation within the delay, and this can lead to bad user experience.

Tags:

Android