开发者

Shared Preferences doesn't seem to save username in session [closed]

开发者 https://www.devze.com 2023-03-10 22:27 出处:网络
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.

This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.

Closed 2 years ago.

Improve this question

For a few days, I have been trying to get the username to save in session properly. First, I opened the app and it would open login screen. I login with username, then it would take me to the MainActivity. Now I exit the app and reopen the app to ensure that username is saved and it would pass the login screen and go straight to the MainActivity. So far so good, then I would logout and it would go back to the login screen. I want to make sure that it is completely logged out so i exit the app and reopen the app and it would take me to the MainActivity. I couldn't figure out开发者_运维知识库 how to fix that problem.

my login class

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("username", username);
        editor.commit();


        if(prefs.getString("username", null)!=username)
        {Intent i = new Intent(getApplicationContext(), Customer.class); 
        startActivity(i);}


        etUsername = (EditText)findViewById(R.id.username);
        btnLogin = (Button)findViewById(R.id.login_button);
        btnCancel = (Button)findViewById(R.id.cancel_button);
        lblResult = (TextView)findViewById(R.id.result);

        btnLogin.setOnClickListener(new OnClickListener() {
            //@Override
            public void onClick(View v) {
            // Check Login
            String username = etUsername.getText().toString();

            if(username.equals("1111")){
                lblResult.setText("Login successful.");

                 Intent i = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(i);

my logout class

public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.logout);

        code = (EditText)findViewById(R.id.codeout);
        btnLogout = (Button) findViewById(R.id.submit);
        btnCancel = (Button) findViewById(R.id.cancel);
        lblResult = (TextView)findViewById(R.id.result);

        btnLogout.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String logout = code.getText().toString();

                if (logout.equals("99")){
                    lblResult.setText("Logout successful");

                    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

                    SharedPreferences.Editor editor = prefs.edit();
                    editor.remove("username");
                    editor.commit();

                    Intent i = new Intent(getApplicationContext(), Login.class);
                    startActivity(i);

                    finish();


Unless I am missing something, it looks like you are setting username in your preferences and then immediately checking that value. It appears that this if statement would always pass:

if(prefs.getString("username", null)!=username)

Also, as @inazaruk pointed out, you should use equals() instead of == for a String comparison.


You should compare the strings using the equals() method, not !=. Also, don't give null as default:

if(!prefs.getString("username", "").equals(username))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号