[英]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.