Home >Java >javaTutorial >How to Access SharedPreferences from an Android PreferenceActivity?
When utilizing a PreferenceActivity to manage application settings, it's common to inflate settings from an XML file. However, accessing the name of the SharedPreference file used by the PreferenceActivity from another Activity can be challenging.
To obtain the SharedPreferences instance from a PreferenceActivity:
import android.preference.PreferenceManager; SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
Use the retrieved SharedPreferences object to access the stored values using methods like:
prefs.getBoolean("keystring", true);
Android provides two types of preferences: SharedPreferences and Activity Preferences.
SharedPreferences:
Activity Preferences:
SharedPreferences can be stored and retrieved using the following methods:
Store:
SharedPreferences.Editor editor = preferences.edit(); editor.putInt("storedInt", value); editor.commit();
Retrieve:
int storedInt = preferences.getInt("storedInt", 0);
The above is the detailed content of How to Access SharedPreferences from an Android PreferenceActivity?. For more information, please follow other related articles on the PHP Chinese website!