简体   繁体   English

屏幕锁定后禁止活动旋转到纵向模式的更好方法是什么?

[英]A better way to prohibit activity rotation to portrait mode after screen is locked?

It is one of Android's idiocies that after the screen is locked while an app/activity is in landscape mode, it will be destroyed and re-created although the user will often keep the device in the same position and will unlock it a short time later. 这是Android的一个独特之处,当屏幕被锁定而应用程序/活动处于横向模式时,它将被销毁并重新创建,尽管用户通常会将设备保持在相同位置并在短时间内解锁。 In this case, after unlocking the screen again, the activity (which was put to portrait mode by Android) will be destroyed and recreated in landscape mode again. 在这种情况下,再次解锁屏幕后,活动(由Android设置为纵向模式)将被破坏并再次以横向模式重新创建。 This is why the user experience is annoying in landscape mode. 这就是用户体验在横向模式下令人讨厌的原因。 Why was this decision made? 为什么做出这个决定? If the user has changed the device orientation, it would be enough to react to this change after unlocking. 如果用户更改了设备方向,则解锁后对此更改做出反应就足够了。

And concerning resources this is a bad idea, too. 关于资源,这也是一个坏主意。 For example, in a camera app, I don't want the camera to be initialized anew as long as the screen is locked. 例如,在相机应用程序中,只要屏幕被锁定,我就不希望重新初始化相机。 This is a good practice for any UI, too. 对于任何UI来说,这都是一个很好的做法。

My current solution looks like this: 我目前的解决方案如下:

public class CameraActivity extends Activity {

    private boolean screenWasLocked;

    public boolean isScreenLocked() {

        KeyguardManager myKM = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
        return myKM.inKeyguardRestrictedInputMode();
    }

    protected void create() {

        setContentView(R.layout.activity_main);

        // ...
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);


        if (isScreenLocked()) return;


        create();
    }

    @Override
    protected void onResume() {

        super.onResume();


        if (isScreenLocked()) {

            screenWasLocked = true;
            return;
        }
        else if (screenWasLocked) {

            screenWasLocked = false;
            create();
        }


        // ...
    }

    // Things you do here must accept that logic in create()/onResume() probably wasn't executed
    @Override
    protected void onPause() {

        // ...

        super.onPause();
    }
}

Can you suggest me a better one? 你能建议我一个更好的吗?

this use. 这个用途。

<activity android:name="variables.App.Splash"
              android:configChanges="orientation|keyboard|keyboardHidden|screenSize|screenLayout|uiMode"
              android:screenOrientation="portrait">

and add to activity 并添加到活动

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

@Override
public void onWindowFocusChanged(boolean hasFocus)
{
    super.onWindowFocusChanged(hasFocus);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当设备处于纵向=屏幕上的可见旋转时,平板电脑上的锁定横向方向+活动开始 - Locked landscape orientation on tablet + activity started when device in portrait = visible rotation on screen 屏幕锁定时活动返回到纵向 - Activity return to Portrait when Screen is Locked CameraX - 在纵向模式下锁定活动时仅旋转预览 - CameraX - Rotate only preview while activity is locked in Portrait mode 如何在活动锁定为纵向模式时检测设备方向更改? - How to detect device orientation changed while activity is locked to portrait mode? 旋转屏幕后刷新活动 - Refresh a activity after a screen rotation Android:活动中的屏幕旋转不会切换纵向/横向布局 - Android: Screen Rotation within Activity doesnt switch portrait/landscape layout 检查设备是否锁定在人像模式 - Check if device is locked in Portrait mode 活动以纵向模式旋转 - activity rotated in portrait mode 即使Android手机处于锁定模式/在锁定屏幕上也要启动活动 - Start an Activity even when Android phone is in locked mode / on lock screen 屏幕旋转后在侦听器中对活动的引用错误 - Wrong reference to Activity in listener after screen rotation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM