注销似乎没有实际注销

时间:2013-08-27 08:27:31

标签: java android login sharedpreferences

我正在开发一个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”活动。

1 个答案:

答案 0 :(得分:0)

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

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

intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

然后在后退按钮上单击,您的应用退出。

同时确保将共享首选项变量刷新为零或调用System.exit()。

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

希望有所帮助