onConfigurationChanged

onConfigurationChanged

本文介绍了一旦使用setRequestedConfiguration,则不会调用onConfigurationChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个活动来处理配置更改,并且它起作用了,这意味着在方向更改时会调用 onConfigurationChanged().

I have set am activity to handle configuration changes and it works, meaning that onConfigurationChanged() is called when the orientation changes.

该活动具有一个按钮,用于显式更改方向.单击后,它会调用 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT).

The activity has a button to explicitly change the orientation. When clicked, it called setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT).

然后不可更改地设置方向,并且不再调用 onConfigurationChanged().

Then the orientation is irrevocably set and onConfigurationChanged() is not called anymore.

当用户单击按钮而不松开 onConfigurationChanged()回调时,如何更改方向?

How can I change the orientation when the user clicks the button while not loosing the onConfigurationChanged() callback ?

推荐答案

这是我解决的方法.我知道它正在重新发明轮子,但是它满足了我的要求,我没有找到使用标准sdk工具处理此问题的合适方法.

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.

首先,创建一个 OrientationManager 类,以侦听方向变化

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);
}
}

然后在您的活动中实现 OrientationChangeListener 并覆盖 onOrientationChanged():

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();
    }
}

因此,我不再使用 onConfigurationChanged ,而是将以下行保留在清单中:android:configChanges ="orientation | screenSize"

So I don't use onConfigurationChanged anymore but keep the following line in the Manifest: android:configChanges="orientation|screenSize"

这篇关于一旦使用setRequestedConfiguration,则不会调用onConfigurationChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 13:16