安卓:编程'允许重新定位'preference仅一次 [英] Android: Programmatic 'allow reorientation' preference works only once

查看:95
本文介绍了安卓:编程'允许重新定位'preference仅一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复选框preference与关键allow_reorientation。如果启用那么我的主要活动应在设备旋转重新调整,如果禁用它应该保持在当前方向。我已将'机器人:configChanges =方向'。在我的manifest.xml允许的方向改变定制处理

I have a CheckBoxPreference with key "allow_reorientation". If enabled then my main activity should reorient upon device rotation, and if disabled it should remain in its current orientation. I have set 'android:configChanges="orientation"' in my manifest.xml to allow custom handling of orientation changes.

从我的主要活动类:

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        if (preferences.getBoolean("allow_reorientation", true)) {
            switch(newConfig.orientation) {
                case Configuration.ORIENTATION_PORTRAIT:
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                    break;
                case Configuration.ORIENTATION_LANDSCAPE:
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                    break;
                default:
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
                    break;
            }
        }
    }

如果我开始在纵向的应用程序,然后将设备旋转为横向,上述code被调用,并根据需要行为。然而,该方法不是在所有旋转回画像时调用,当然视图保持在横向

If I start my application in portrait, then rotate the device to landscape, the above code is called and behaves as desired. However, the method is not called at all when rotating back to portrait, and of course the view remains in landscape.

我的第一个猜测是因为我不保存newConfig.orientation'的任何地方,第二个方向调整回画像比较打击仍然表示纵向一pre-现有价值取向的最新值,因此,没有检测到配置更改。但是,如果这是真的,那么我在上面的方法设置一个断点会当我尝试定向景观第二次,但该方法从来没有在这种情况下所谓的激活。

My first guess was that since I don't store the 'newConfig.orientation' anywhere, the second reorientation back to portrait was comparing the latest orientation value against a pre-existing value that still indicated portrait orientation, and thus no configuration change was detected. However, if this were true then a breakpoint I've set in the above method would be activated when I attempt to orient to landscape a second time, yet the method is never called in this circumstance.

可能有人请阐明这是怎么回事,并提供补救措施?

Could someone please elucidate what's going on, and offer a remedy?

非常感谢。

SOLUTION - 感谢去下面马修·威利斯

在YourApp延伸应用:

In YourApp which extends Application:

public void updateOrientationConfiguration(Activity activity) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    if (preferences.getBoolean("allow_reorientation", true)) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
    } else {
        if (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        if (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }
}

在YourActivity延伸活动:

In YourActivity which extends Activity:

    public void onResume() {
        super.onResume();
        YourApp app = (YourApp)this.getApplication();
        app.updateOrientationConfiguration(this);
    }


推荐答案

的另一种方法是使用<一href=\"http://developer.android.com/reference/android/content/pm/ActivityInfo.html#SCREEN_ORIENTATION_SENSOR\">ActivityInfo.SCREEN_ORIENTATION_SENSOR和<一个href=\"http://developer.android.com/reference/android/content/pm/ActivityInfo.html#SCREEN_ORIENTATION_NOSENSOR\">ActivityInfo.SCREEN_ORIENTATION_NOSENSOR.

An alternative method is to use ActivityInfo.SCREEN_ORIENTATION_SENSOR and ActivityInfo.SCREEN_ORIENTATION_NOSENSOR.

您基本上会设置一个或另一个根据您的preference,然后你不必实施 onConfigurationChanged (或一组 configChanges 在你的清单)。

You would basically set one or the other based on your preference and then you don't have to implement onConfigurationChanged (or set configChanges in your manifest).

一个例子活动:

public class Main extends Activity {
    private static boolean locked = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        doLock();
    }

    public void doLock() {
        if (locked) {
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            }
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
        }
    }

    public void toggle(View v) {
        locked = !locked;
        doLock();
    }
}

在测试中,我注意到 SCREEN_ORIENTATION_NOSENSOR 也导致旋转到默认方向,所以需要使用 _LANDSCAPE _PORTRAIT 明确。

In testing I noticed that SCREEN_ORIENTATION_NOSENSOR also causes a rotate to the default orientation, so it is necessary to use _LANDSCAPE and _PORTRAIT explicitly.

这篇关于安卓:编程'允许重新定位'preference仅一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆