简体   繁体   中英

SharedPreferences don't work

i'm having little problems using the SharedPreferences. I want to save and later load strings to/from the Preferences. I initialize my prefs in the onCreate-method:

prefs = this.getSharedPreferences("com.example.android_test", Activity.MODE_PRIVATE);

I do saving in another method:

    public void saveUser()
    {   
        prefs.edit().putString("username", username);
        prefs.edit().putString("password", password);
        prefs.edit().apply();
    }

And loading in yet another method:

    public void loadUser()
    {
        username = prefs.getString("username", "default");
        password = prefs.getString("password", "test");
    }

And those are my Test-Methods:

    public void showUser(View v) 
    {
       loadUser();
       text.setText(username);
    }

    public void addUser(View v)
    {
        changeUser(eingabe.getText().toString(),"newpass");
    }

    public void changeUser(String user, String pass)
    {
        username = user;
        password = pass;
        saveUser();
    }

username and password are global, private Strings, eingabe is an EditText and text is a TextView. However, when executing showUser() i only get the defaultvalue displayed to the TextView, even if im using saveUser with different usernames... No crash or anything... its just only the defaultvalue being shown...

您忘记了进行编辑:

prefs.edit().putString("username", username).commit();
 prefs.edit().putString("username", username);
 prefs.edit().putString("password", password);

Change this each time edit will return diffrent instance so your changes wont get committed.

Editor editor = prefs.edit();
editor.putString("username", username);
editor.putString("password", password);
editor.commit();

Try this.

Unlike commit(), which writes its preferences out to persistent storage synchronously, apply() commits its changes to the in-memory SharedPreferences immediately but starts an asynchronous commit to disk and you won't be notified of any failures. If another editor on this SharedPreferences does a regular commit() while a apply() is still outstanding, the commit() will block until all async commits are completed as well as the commit itself.

Ok, apply is same as commit , it only writes the preferences atomically making it thread safe. (It requires API Level 9 though, so be careful)

See the documentation

The problem is what Rajesh CP is saying. You need to create an editor pointer and then apply:

Editor editor = prefs.edit();
editor.putString("username", username);
editor.putString("password", password);
editor.apply();

Else, every time you create a new instance of the editor and when you apply the changes, the editor that is being instructed to apply the changes, doesn't have any changes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM