在我的应用程序中,我有一个登录和注销机制。我想在用户按下注销按钮时清除任务堆栈,这样当他再次启动应用程序时,他将再次登录。我在网上查了一下,大多数情况下人们都用这个来完成它:
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();
使用此代码会发生这种情况:
home->登录活动(主启动器活动)[uername和密码字段为空] - >点击退出 - >主页 然后当我这样做时:
来自home->启动应用程序(在登录屏幕中,用户名和密码仍然存在) - >按后退按钮导航到主页 - >再次启动应用程序 - >用户名和密码已清除
什么是编写注销功能的更好方法?
答案 0 :(得分:0)
您可以在共享首选项数据库中保存用户名和密码。当用户注销时,可以在同一个键中清除数据库中的值。
如果用户直接退出应用而没有注销,请检查用户名和密码是否已存在,您可以将其显示在edittext中。
保存在数据库中
SharedPreferences settings = getSharedPreferences("DB_NAME", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("user", username);
editor.putString("pass", password);
editor.commit();
从数据库中清除
SharedPreferences settings = getSharedPreferences("DB_NAME", 0);
SharedPreferences.Editor editor = settings.edit();
editor.remove("user");
editor.remove("pass");
editor.clear();
editor.commit();
答案 1 :(得分:0)
重写Application类并创建一个公共字段(或使用getter / setter私有)。
在应用程序类中:
public boolean loginDialogShown = false;
在登录对话框代码中:
MyApplication.loginDialogShown = true;
活动:
if (!MyApplication.loginDialogShown){ loginDialog.show(); }