Ted pottel
Ted pottel

Reputation: 6983

Android trying to use Preferences to save data

I’m trying to learn how Preferences work by writing a short program to store ted in the key board and then load the value in boardstr. When I use getString to load the value, the value of boardstr does not change.

boardstr= new String();      
boardstr="fred";
// set the prefrence to ted
this.getPreferences(MODE_PRIVATE).edit().putString("board","ted");
// kload the prfrence in boardstr
this.getPreferences(MODE_PRIVATE).getString("board",boardstr);
// boardstr stil equals fred, not ted 

Upvotes: 0

Views: 1753

Answers (1)

eldarerathis
eldarerathis

Reputation: 36193

You need to either commit() or apply() the changes in order for them to be saved and take effect.

E.g.:

// set the prefrence to ted
this.getPreferences(MODE_PRIVATE).edit().putString("board","ted");
this.getPreferences(MODE_PRIVATE).edit().apply();

Upvotes: 4

Related Questions