I'm trying to use the Android PreferenceScreen
as a convenient way to layout my user settings, but I don't want to save the preferences to the device shared preferences. Is this possible, or should I be using a different mechanism such as ListView
?
It seems really convenient to use the PreferenceScreen
type because I require different widgets (ie. switches, edit text). But I'm already running into problems with persistence, ie. anything I enter is persisted even across sessions when I don't want it to be.
settings.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Preference
android:key="settings_type"
android:title="@string/label_type" />
<EditTextPreference
android:key="settings_edit_name"
android:title="@string/label_name"
android:dialogTitle="Enter a name"
android:singleLine="true" />
<SwitchPreference
android:key="settings_edit_state"
android:title="@string/label_state"
android:summary="Enable or disable the state" />
</PreferenceScreen>
Preferences: The user interfaces part of the settings. It contains different classes which allow one to compose Settings screens from code or XML. Shared Preferences: These are used to store values in XML files. These files are created, maintained and deleted by Android for you.
Represents a top-level Preference that is the root of a Preference hierarchy. A PreferenceActivity points to an instance of this class to show the preferences. To instantiate this class, use PreferenceManager#createPreferenceScreen(Context) .
Android Shared Preferences Overview Android stores Shared Preferences settings as XML file in shared_prefs folder under DATA/data/{application package} directory. The DATA folder can be obtained by calling Environment.
I was storing a String value in SharedPreferences that was setting the value for a Dropdownmenu . Now I have updated those values so the previous are no longer valid. However, even after uninstalling the app and clearing the data the old shared preference value is still there.
I know that's an old question but I find it quite interesting. Just set this attribute to your preference and it won't be saved:
android:persistent="false"
I re-read the appropriate docs and determined how to prevent saving preferences by implementing the Preference.OnPreferenceChangeListener
interface.
public static interface Preference.OnPreferenceChangeListener
Interface definition for a callback to be invoked when the value of this Preference has been changed by the user and is about to be set and/or persisted. This gives the client a chance to prevent setting and/or persisting the value.
Example:
public class SettingsFragment extends PreferenceFragment {
public static SettingsFragment newInstance(int index) {
SettingsFragment f = new SettingsFragment();
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.settings);
// getArguments().getInt("index");
EditTextPreference namePreference = (EditTextPreference) findPreference("settings_edit_name");
namePreference.setOnPreferenceChangeListener(new NamePreferenceChangeListener());
}
private class NamePreferenceChangeListener implements Preference.OnPreferenceChangeListener {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// Do something else presumably and then return false to avoid saving the pref.
return false;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With