s.d
s.d

Reputation: 29436

Initializing Preferences

Android Guide recommends defining preferences in XML files, And from there, these can be loaded in PreferenceActivity/PreferenceFragment etc for viewing and editing by user. But in real scenario, User Interacts with other activities first, then (maybe) with Preferences UI.

What if the starter activity needs some of these preferences ? They'll be not loaded yet, because preferences resources has not been inflated yet. Is there a way to pre-access preferences in XML files ?

Upvotes: 1

Views: 198

Answers (2)

Aaron Fujimoto
Aaron Fujimoto

Reputation: 321

Yes. When you first request the preference you can provide it with a default value. E.g. if you are loading a preference of type Int, then you can do so in the following manner from an activity:

SharedPreferences defaultSettings = PreferenceManager.getDefaultSharedPreferences(this);
int preferenceValue = defaultSettings.getInt("PreferenceName", 7);

This would load your preferenceValue to be 7 (without this preference ever being initialized yet). This is assuming that in your XML preference file, you have a preference of key "PreferenceName". If you plan on editing this preference in the activity before the Preference activity has been ran, be sure you commit your changes with a SharedPreferenceEditor:

// ... change to preferenceValue occurs prior to this code
SharedPreferences.Editor defaultEditor = defaultSettings.edit();
defaultEditor.putInt("PreferenceName", preferenceValue);
defaultEditor.commit();

We probably want to avoid "PreferenceName" in a hardcoded matter though, and instead use it as a string in the strings.xml file. This way it can be grabbed both from the initial code when the preference has not been saved yet and from the Preference XML file as well. This means that our above code would substitute the string "PreferenceName" with something like the following:

getResources().getString(R.string.pref_name)

And in your Preference XML file you may would reference the key in the following way:

android:key="@string/pref_name"
android:defaultValue="7"

This should cover "pre-loading" the preference as well as trying to keep most of the application settings within one place. There may indeed be overlap in terms of whether or not the XML preference was created/loaded before the initial Activity occurred, but I haven't tested that out yet.

EDIT: It turns out instead of using the above code, you can directly load the XML file (with its default preference) by the following method:

PreferenceManager.setDefaultValues(this, R.xml.preference, false);

More information about this method can be found in the documentation for the PreferenceManager: http://developer.android.com/reference/android/preference/PreferenceManager.html

Upvotes: 3

Infinity
Infinity

Reputation: 3875

If you look at SharedPreference API, you will see this

getString(String key, String defValue)

So, you can actually in fact define a default value if it's not already existed.

Source: http://developer.android.com/reference/android/content/SharedPreferences.html

You can also predefine default value in XML using

android:defaultValue="SOMETHING"

Upvotes: 0

Related Questions