登出用户 Java

huangapple go评论56阅读模式
英文:

Logging user out Java

问题

我正在创建一个带有登录/注册系统的社交应用,但我在登出用户时遇到问题。

当我点击登出按钮时,我希望取消用户名并清除用户的整个会话,以便他们返回到“LoginActivity”类。当前情况是,当我转到个人资料页面并点击登出时,我直接返回到主页活动(Home Activity),但主页活动只应该供已登录的用户使用。我从昨天开始一直在尝试,但仍然没有结果。有人可以帮助我吗?

登录活动:

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 = 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);

    LoginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String username = LoginUsername.getText().toString();
            String pw = LoginPassword.getText().toString();

            editor.putString("username", username);
            editor.putString("pw", pw);
            editor.apply();

            SendUserToHomeActivity();
        }
    });

    NeedNewAccountLink.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Handle creating a new account here
        }
    });
}

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(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit().clear().apply();

        SharedPreferences sharedPreferences = getSharedPreferences(PREF_LOGIN, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        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);
    }
});

请注意,上述代码示例仅包含翻译部分,不包含任何额外内容。如果需要进一步的解释或帮助,请随时提问。

英文:

I am creating a social app with a login/Registration system and I am having trouble logging the user out.

When I click the logout button I want to unset the username and clear the entire session of the user so that they go back to the LoginActivity class. Right now when I go to the profile activity and click logout I go straight back to the Home Activity which is only suppose to be for user who are logged in. I've been trying since yesterday and still nothing. Can someone help me ?

Login activity:

//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);
}

profile activity:

    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);
});

答案1

得分: 1

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);

});

Remove the `Intent.FLAG_ACTIVITY_CLEAR_TOP` flag will solve your problem. Please refer to this [link][1].
英文:
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);
});

Remove the Intent.FLAG_ACTIVITY_CLEAR_TOP flag will solve your problem. Please refer to this link.

答案2

得分: 0

我相信要从编辑框中获取文本,你需要使用以下代码:

LoginUsername.getText().toString()

并且检查 EditText "LoginUsername" 是否符合条件将不会产生 null,除非它指向错误的 ID。

请尝试在下面的代码块中进行适当的更改:

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();
}
英文:

I believe to get text from edit text you need
> LoginUsername.getText().toString()

and Checking EditText "LoginUsername" against If condition will never yield null unless
it points to wrong ID

Try proceeding with appropriate changes in below code block

    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();
}

huangapple
  • 本文由 发表于 2020年9月15日 11:45:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63894763.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定