Created
March 11, 2013 09:42
-
-
Save jgilfelt/5133095 to your computer and use it in GitHub Desktop.
Android - Set default preference values for a modern fragment-based PreferenceActivity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Since this code is in
Application
, you could replacegetApplicationContext()
withthis
, in all occurrences.