【问题标题】:Logging out seems not to actually log out注销似乎并没有真正注销
【发布时间】:2013-08-27 08:27:31
【问题描述】:

我正在开发一个用户需要注册/登录的 android 应用程序,这很好用,但是当用户退出时,问题就出在哪里。

当前,当用户按下注销按钮时,它确实会将他们带回登录页面,但是如果您离开应用程序然后返回到它(因此它仍在内存中)而无需再次登录,它将带您返回到您登录后看到的页面。

我使用了一个共享偏好来记录用户是否登录,然后有一个启动活动,这是开始决定要显示哪个屏幕的第一件事:

public class Splash extends SherlockActivity {
    public static final String PREFS_NAME = "PrefsFile";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        boolean loggedin = settings.getBoolean("loggedIn", false);
        if (loggedin){
            Intent intent = new Intent(this, MyLists.class);
            startActivity(intent);
        }
        else{
            Intent intent = new Intent(this, LogIn.class);
            startActivity(intent);
        }
    }

}

然后我的注销按钮看起来像

private OnClickListener OnClick_logout = new OnClickListener() {
        public void onClick(View v) {
            SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("loggedIn", false);
            editor.putString("email", "");
            editor.putString("password", "");
            editor.commit();
            db.clearLists();
            db.clearProducts();
            Intent intent = new Intent(v.getContext(), Splash.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            v.getContext().startActivity(intent);
        }
    };

按下按钮后,启动活动会将用户带到当前的登录屏幕,但就像我说的那样,如果您随后关闭应用程序并返回它,它将把用户带到“MyLists”活动。

【问题讨论】:

  • 你有什么问题..?
  • 如果您随后关闭应用程序并返回它,它会将用户带到“MyLists”活动。无需实际再次登录
  • 你能把LogIn.java的代码贴出来吗?

标签: java android login sharedpreferences


【解决方案1】:

您需要使用意图标志来清除堆栈历史记录。在您导航回登录屏幕之前,请说

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

然后单击后退按钮,您的应用程序将退出。

还要确保将 sharepreference 变量刷新为零或调用 System.exit()。

P.S : 调用 system.exit() 将使用户在每次他/她想要使用该应用程序时登录。

希望有帮助

【讨论】:

  • 因此,如果我将这些标志添加到注销按钮中的意图中,它应该可以工作还是?
  • 是的,试着告诉我
  • 它仍然做同样的事情,当你回到应用程序时,它会直接进入 mylists 活动
猜你喜欢
  • 2016-12-25
  • 2021-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-08
  • 1970-01-01
  • 2018-01-14
  • 2011-11-14
相关资源
最近更新 更多