Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset default values of Preference

I am using CheckBoxPreference for settings screen. The XML is:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen  xmlns:android="http://schemas.android.com/apk/res/android" >
    <CheckBoxPreference android:key="includeAddress"
                    android:title="Include Address" 
                    android:summary=""
                    android:defaultValue="true" />
    <CheckBoxPreference android:key="rememberName"
                    android:title="Remeber Name" 
                    android:summary=""
                    android:defaultValue="false" />
</PreferenceScreen>

I change the values while in the application. Once the user logs out, it must be set to default values as defined in the xml. But, it does not seem to work. They keep those values I chose last.

Having read Android docs, I found this:

PreferenceManager.setDefaultValues(getApplicationContext(), R.xml.preference_settings, true);

But it hardly does the job! Tried other way round with SharedPreferences.

SharedPreferences preferences = getParent().getSharedPreferences("preference_settings", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.commit();

It didn't work either!

Am I missing something? How could I set preferences to their default values defined in the XML?

Thanks in advance!

like image 258
Renjith Avatar asked Dec 07 '12 11:12

Renjith


1 Answers

the shared preferences should work, but you should use the default shared preferences..

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.commit();

To get the shared preferences using file name, Android creates this name (possibly based on the package name of your project?). You can get it by putting the following code in a SettingsActivity onCreate, and seeing what preferencesName is.

String preferencesName = this.getPreferenceManager().getSharedPreferencesName();

The string should be something like "com.example.projectname_preferences". Hard code that somewhere in your project, and pass it in to getSharedPreferences() and you should be good to go.

AS:

 PreferenceManager.getDefaultSharedPreferences(this);

Will provide an access to a preferences file that is global for the whole application package ; any activity can access the preferences (internaly, the xml file holding the preferences will be named your.application.package_preferences.xml).

getParent().getSharedPreferences("preference_settings", MODE_PRIVATE);

Will provide preferences only for the contextInstance class: only instances of the context's class can access these preferences (said your package is still your.application.package and you're in your.application.package.SecondActivity, internaly the preferences file is SecondActivity.xml).

like image 89
Nermeen Avatar answered Sep 28 '22 01:09

Nermeen