Reputation: 3
Please help.. I am stacked with my thesis.. Here I use Shared Preferences, which works well between 2 activities. However, I want to keep the data available for multiple activities. How do I do that?
The way that I save:
SharedPreferences example = getSharedPreferences(GAME_PREFERENCES_LOGIN, 0);
Editor editor = example.edit();
editor.putString("username", username.getText().toString());
editor.commit();
The way that I retrieve:
SharedPreferences example = getSharedPreferences(GAME_PREFERENCES_LOGIN, 0);
String userString = example.getString("username", "null");
tv.setText("Welcome " +userString);
How can I obtain this value from another activity and another activity, without having to save again and again this single value? Thank you!!
Upvotes: 0
Views: 63
Reputation: 199805
As long as you are retrieving the SharedPreferences
using the same name, then the same SharedPreferences instance (and all of the values that have been committed so far) is returned as per the documentation, whether it is from a new Activity
or the one that created the shared preference key.
Upvotes: 2