Reputation: 6983
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
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