java - 注销用户 Java
问题描述
我正在创建一个带有登录/注册系统的社交应用程序,但我无法将用户注销。
当我单击注销按钮时,我想取消设置用户名并清除用户的整个会话,以便他们返回LoginActivity
课堂。现在,当我转到个人资料活动并单击注销时,我会直接返回主页活动,该活动仅适用于已登录的用户。我从昨天开始一直在尝试,但仍然没有。有人能帮我吗 ?
登录活动:
//SharedPreferences preferences;
private ProgressDialog loadingBar;
private Button LoginButton;
private EditText LoginUsername, LoginPassword;
private TextView NeedNewAccountLink;
private static final String PREF_LOGIN = "LOGIN_PREF";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(PREF_LOGIN, MODE_PRIVATE);
SharedPreferences sharedPreferences = getSharedPreferences(PREF_LOGIN, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
}
LoginButton = (Button) findViewById(R.id.login_button);
LoginUsername = (EditText) findViewById(R.id.login_username);
LoginPassword = (EditText) findViewById(R.id.login_password);
NeedNewAccountLink = (TextView) findViewById(R.id.need_new_account_link);
loadingBar = new ProgressDialog(this);
editor.putString("username", String.valueOf(LoginUsername));
editor.putString("pw", String.valueOf(LoginPassword));
editor.apply();
if (LoginUsername != null) {
editor.remove("username");
editor.remove(String.valueOf(sharedPreferences));
editor.remove("pw");
editor.clear();
editor.apply();
SendUserToHomeActivity();
}
private void SendUserToHomeActivity() {
Intent mainIntent = new Intent(LoginActivity.this, HomeActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(mainIntent);
finish();
}
public void OnLogin(View view) {
String username = LoginUsername.getText().toString();
String pw = LoginPassword.getText().toString();
String type = "login";
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type, username, pw);
}
个人资料活动:
LogoutButton.setOnClickListener(view -> {
PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit().clear().apply();
sharedPreferences.edit().remove("username").apply();
sharedPreferences.edit().remove("pw").apply();
editor.remove("username");
editor.remove("pw");
editor.clear();
editor.apply();
finish();
Intent intent = new Intent(ProfileActivity.this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
});
解决方案
LogoutButton.setOnClickListener(view -> {
PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit().clear().apply();
sharedPreferences.edit().remove("username").apply();
sharedPreferences.edit().remove("pw").apply();
editor.remove("username");
editor.remove("pw");
editor.clear();
editor.apply();
Intent intent = new Intent(ProfileActivity.this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
});
删除Intent.FLAG_ACTIVITY_CLEAR_TOP
标志将解决您的问题。请参考此链接。
推荐阅读
- javascript - 在 div 中加载时加载进度条不起作用
- elixir - Elixir/Phoneix - SFTPClient 连接错误
- css - 用于缩放高分辨率的媒体
- xml - 模板和xml
- python - 同时运行多个 Google Colab Notebook
- mysql - “Azure Defender for MySQL”和“Azure Database for MySQL 的高级威胁防护”之间的区别
- python - Seaborn 联合图用相关性注释
- react-native - 如何在新的 React Native 架构中调试 javascript?
- javascript - 函数未在管道中的地图内执行
- idempiere - 在语言中更新格式时,不会发生 Idempiere 7.1 中显示和输入日期格式的更改