Kozlov
Kozlov

Reputation: 554

Preference not getting initialised with default values from the xml

This question has been asked many times in this forum. But I feel it still need to be cleared for me .

public class PrefTest extends Activity {
  public Button bt_start= null;
  SharedPreferences mSharedPreferences;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {

    setContentView(R.layout.main);


    bt_start = (Button) findViewById(R.id.button1);
    bt_start.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
      Log.d("TEST","");
      PreferenceManager.setDefaultValues(getApplicationContext(),
                         R.xml.settings_org, true);
      mSharedPreferences = PreferenceManager
        .getDefaultSharedPreferences(getApplicationContext());
      Boolean test = false;
      test = mSharedPreferences.getBoolean("auto_launch_key", true);
      Log.d("TEST","test = "+test);
    }
      });
    super.onCreate(savedInstanceState);
  }

  @Override
  protected void onStart() {
    super.onStart();
  }
}

In the above code

   Log.d("TEST","test = "+test);

always prints true , though I have set default value in xml as false(as below)

 <?xml version="1.0" encoding="utf-8"?>
 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="settings" >
    <CheckBoxPreference android:key="auto_launcvh_key"
        android:summaryOn="..."
        android:summaryOff="---"
        android:title="auto_launch_string" android:defaultValue="false" />
</PreferenceCategory>
 </PreferenceScreen>

Am expecting the setDefaultValues to take the defaults values form the XML and initialize the preference.

Am I wrong in my understanding?

Upvotes: 2

Views: 1865

Answers (2)

Elias DC
Elias DC

Reputation: 608

The answer is actually very simple. Boolean values in a preference file are only present when they are true so when reading the boolean value you need to set default value to false:

test = mSharedPreferences.getBoolean("auto_launch_key", false);

So when the preferences in xml is true it reads true and otherwise uses the default of getBoolean. I noticed while debugging there aren't any Boolean preferences with false as the value.

Upvotes: 1

rogermushroom
rogermushroom

Reputation: 5586

UPDATE

On closer inspection I can see you probably aren't correctly retrieving the SharedPreference object. although I have not tried it, to make this test work I assume you would have to call getSharedPreferences (String name, int mode) with the name of your XML file to get the object which stores the values defined in your XML file.

getDefaultSharedPreferences (Context context) states:

Gets a SharedPreferences instance that points to the default file that is used by the preference framework in the given context. Parameters

context The context of the preferences whose values are wanted. Returns

A SharedPreferences instance that can be used to retrieve and listen to values of the preferences.

Your file doesn't seem to be the default file and thus the preferences you are trying to call don't exist.

Generally the way to deal with preferences is to sub class a PreferenceActivity which will create a preferences interface but I appreciate you are just trying to write a simple test.

Also I assume the CheckBoxPreference android:key="auto_launcvh_key"is a typo when writing the question. I think I checked the key when first writing this answer and it was CheckBoxPreference android:key="auto_launch_key"

ORIGINAL

Firstly I would change test = mSharedPreferences.getBoolean("auto_launch_key", true); to test = mSharedPreferences.getBoolean("auto_launch_key", false); if it now returns false then the preference doesn't exist in the system so there is a problem with your environment. Try cleaning the project and re-installing.

Secondly in the docs it states

readAgain - Whether to re-read the default values. Note: this will NOT reset preferences back to their default values. For that functionality, use getDefaultSharedPreferences(Context) and clear it followed by a call to this method with this parameter set to true.

Therefore if the preference already exists in the system as true this will not overwrite and so a re-installation should sort this out as well. or you could try calling clear() on mSharedPreferences and then calling PreferenceManager.setDefaultValues(getApplicationContext(), R.xml.settings_org, true);

If this doesn't work can you post complete XML.

Upvotes: 2

Related Questions