Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android PreferenceScreen - Can I use it without saving preferences to SharedPreferences?

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>
like image 933
DanielB6 Avatar asked Jan 31 '13 19:01

DanielB6


People also ask

What is difference between preferences and SharedPreferences?

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.

What is Preferencescreen in Android?

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) .

Where shared preferences are stored in Android device?

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.

Does SharedPreferences persist after uninstall?

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.


2 Answers

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"
like image 82
Romain Piel Avatar answered Sep 18 '22 07:09

Romain Piel


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;
    }

}
like image 39
DanielB6 Avatar answered Sep 21 '22 07:09

DanielB6