Reputation: 18130
I have two activities. In the first activity, I'm putting a String into a shared preference. I then log the getString and I see that it shows up. I then move onto the second activity, and I Toast the getString and I get the default value that shows up.
The first activity code:
SharedPreferences.Editor pref_editor = mcontext.getSharedPreferences("Prefs", Context.MODE_PRIVATE).edit();
SharedPreferences pref = mcontext.getSharedPreferences("Prefs", Context.MODE_PRIVATE);
pref_editor.putString("test", "It works!").commit();
Log.d("XXX", pref.getString("test", "ERRRROR"));
The second activity code:
SharedPreferences pref = mcontext.getSharedPreferences("Prefs", Context.MODE_PRIVATE);
String current = pref.getString("test", "ERROR");
Toast.makeText(getApplicationContext(), current,
Toast.LENGTH_SHORT).show();
Any idea why I'm getting the default value of "ERROR" when I toast?
Upvotes: 1
Views: 117
Reputation: 1729
Please try this:-
SharedPreferences pref = mcontext.getSharedPreferences("Prefs", Context.MODE_PRIVATE);
SharedPreferences.Editor pref_editor = pref.edit();
pref_editor.putString("test", "It works!")
pref_editor.commit();
Upvotes: 2
Reputation: 93559
You aren't calling commit on the editor. The changes are batched and not written to disk until commit is called.
Upvotes: 0