在屏幕旋转的Andr​​oid prevent登录界面 [英] Prevent login screen on screen rotation android

查看:82
本文介绍了在屏幕旋转的Andr​​oid prevent登录界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在登录屏幕显示了某项活动去的onPause状态的应用程序。一般情况下,当屏幕方向改变时,活动进入的onPause状态,所以我莫名其妙prevented登录屏幕时,设备旋转。请参见下面的code,

I have an application in which a login screen shows up whenever an activity goes to onPause state. Generally, when screen orientation changes, the activity goes to onPause state, so I somehow prevented login screen when device is rotated. See the code below,

protected void onPause() {
    super.onPause();

    WindowManager mWindowManager =  (WindowManager) getSystemService(WINDOW_SERVICE);
    mDisplay = mWindowManager.getDefaultDisplay();

    mOrientation = mDisplay.getRotation();

    if(mOrientation == 1 || mOrientation == 2 || mOrientation == 3 || mOrientation == 0)
    {
        inApp = true;
    }
    if (!inApp) {

        SavedState.setState(this, "HomeActivity");

        Intent intent = new Intent(HomeActivity.this, LoginActivity.class);
        startActivity(intent);
    }
}

但问题是,当我preSS home键,回来到应用程序,登录屏幕没有显示出来,而不是它是直接恢复到活动中,因为 mDisplay.getRotation() 读屏当前方向和如果条件始终为真。

But the problem is when i press home button and comes back to the application, the login screen is not showing up instead it is directly resuming to the activity, because mDisplay.getRotation() reads the screen current orientation and if condition always becomes true.

要说得简洁明了,我需要的登录界面中显示,当用户presses Home键或切换到其他的应用程序,但不是当屏幕旋转。

To put it simple and clear, I need the login screen to be shown when the user presses Home button or switches to other application but not when the screen is rotated.

任何一种建议或例子很多AP preciated。谢谢!

Any kind of suggestion or example is much appreciated. Thanks !

推荐答案

这里的东西应该工作 - 你可能要割肉出来有点...

Here's something that should work -- you might have to flesh it out a bit...

class MyActivity... {
    private boolean loggedIn = false;

@Override
protected void onSaveInstanceState( Bundle data ) {
    super.onSaveInstanceState( data );
    data.putBoolean( "loggedIn", loggedIn );
}

@Override
protected void onUserLeaveHint() {
    isLoggedIn = false; // user pressed home
}

@Override 
protected void onCreate( Bundle data ) {
    isLoggedIn = data.getBoolean( "loggedIn", false );
    ....
}

@Override
protected void onResume() {
    if( !isLoggedIn ) {
        /// Log in...
    }
}

此模式使用/恢复使用preferred法活动的状态,作为奖励,注销用户,如果Home键为pressed(或用户活动决定移动到不同的应用程序)。需要注意的是电话呼叫不应注销用户在这里 - 在大多数情况下是所期望的行为

This pattern uses/restores the state of the activity using the preferred method, and as a bonus, logs out the user if the Home button is pressed (or the user activity decides to move to a different app). Note that phone-calls should not log the user out here -- which in most cases is the desired behavior.

请确保您删除机器人:configChanges 垃圾,别人推荐的

Make sure you remove the android:configChanges garbage that someone else recommended.

这篇关于在屏幕旋转的Andr​​oid prevent登录界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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