Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve the default value of a preference defined in the XML file

How do you get the default value of a single Android Shared Preference as it is explicitly defined in the corresponding XML file? E.g.:

<CheckBoxPreference
    android:defaultValue="false"
    android:key="fulldb"
    android:summary="No selection rules apply"
    android:title="Use Full Database" />
like image 996
halxinate Avatar asked Nov 13 '22 12:11

halxinate


1 Answers

Like this..

public class Calc extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";

@Override
protected void onCreate(Bundle state){
   super.onCreate(state);
   . . .

   // Restore preferences
   SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
   boolean silent = settings.getBoolean("silentMode", false); //default value if nothing is in the preference is the last parameter false.
   setSilent(silent);
}

@Override
protected void onStop(){
   super.onStop();

  // We need an Editor object to make preference changes.
  // All objects are from android.context.Context
  SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
  SharedPreferences.Editor editor = settings.edit();
  editor.putBoolean("silentMode", mSilentMode);

  // Commit the edits!
  editor.commit();
}

}

like image 85
coder_For_Life22 Avatar answered Nov 16 '22 04:11

coder_For_Life22