Skip to content

Instantly share code, notes, and snippets.

@jgilfelt
Created March 11, 2013 09:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jgilfelt/5133095 to your computer and use it in GitHub Desktop.
Save jgilfelt/5133095 to your computer and use it in GitHub Desktop.
Android - Set default preference values for a modern fragment-based PreferenceActivity
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// we cannot call setDefaultValues for multiple fragment based XML preference
// files with readAgain flag set to false, so always check KEY_HAS_SET_DEFAULT_VALUES
if (!PreferenceManager.getDefaultSharedPreferences(getApplicationContext())
.getBoolean(PreferenceManager.KEY_HAS_SET_DEFAULT_VALUES, false)) {
PreferenceManager.setDefaultValues(getApplicationContext(), R.xml.pref_general, true);
PreferenceManager.setDefaultValues(getApplicationContext(), R.xml.pref_labels, true);
PreferenceManager.setDefaultValues(getApplicationContext(), R.xml.pref_appearance, true);
}
}
}
@commonsguy
Copy link

Since this code is in Application, you could replace getApplicationContext() with this, in all occurrences.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment