首页 > 解决方案 > 注销用户 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);

    });

标签: javaandroidandroid-studio

解决方案


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标志将解决您的问题。请参考此链接


推荐阅读