2

I am using SharedPreferences to keep the information about user's weight, which I need in my application. The problem is, how to set a default value (eg. 75 kg) automatically after installation? I know how to do it via .xml, but how to do this programmatically?

My code:

public class SettingsDialogFragment extends DialogFragment{

public static final String PREFS_NAME = "settings";
public Dialog onCreateDialog(Bundle savedInstanceState) {


builder.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {

                Data.weight = weight;
                SharedPreferences prefs = getActivity().getSharedPreferences(PREFS_NAME, 0);
                Editor editor = prefs.edit();
                editor.putInt("key_weight", weight);
                editor.commit();
                Data.ifMale = ifMale;
                checkedRadio = rg.getCheckedRadioButtonId();
                System.out.println("numer radio" +checkedRadio);
            }
            });
return builder.create();
    }
}
fragon
  • 3,391
  • 10
  • 39
  • 76
  • 1
    Check to see if it is the first time your app is run and if so set the default preference to whatever you want it to be. I think that is what you are asking. – zgc7009 Nov 05 '14 at 19:28
  • The SharePreferences API also allows you to get default values if the key isn't in the list, so you may not need to set defaults to begin with. – DeeV Nov 05 '14 at 19:29
  • @zgc7009 Yes. That's what I want to achieve. On the first start of the application I want the weight value to be set to eg. 75 kg (instead of 0 or null), so that some algorithms are counted correctly. Could you please suggest any way to achieve this? EDIT: I've found this solution: http://stackoverflow.com/a/13237848/1828352 , but my question is what happens if the user updates the app via Google Play? The value will be set to default one more time or not? – fragon Nov 05 '14 at 19:34
  • 1
    @6franek updates shouldn't mess with your app's shared preferences. It will only get reset if you uninstall the app and reinstall it. – zgc7009 Nov 05 '14 at 19:40

2 Answers2

4

Try this way, please.

        SharedPreferences prefs = getActivity().getSharedPreferences(
                PREFS_NAME, 0);
        if (prefs.getInt("key_weight", null) == null) {
            Editor editor = prefs.edit();
            editor.putInt("key_weight", 75);
            editor.commit();
        }

For first time use this, or else use your code only(means without if condition).

Chitrang
  • 5,097
  • 1
  • 35
  • 58
0

getInt takes a default value.

prefs.getInt("key_weight", 75)

Or in a more mainstream style....

public class AppPreferences {

    private SharedPreferences mPreferences;

    Public AppPreferences(SharedPreferences preferences)
    {
         this.mPreferences = preferences;
    }

    private static final String KEY_WEIGHT_KEY = "key_weight";
    private static final int DEFAULT_KEY_WEIGHT = 75;

    public static  int getKeyWeight()
    {
      return mPreferences.getInt(KEY_WEIGHT_KEY,DEFAULT_KEY_WEIGHT);

    }
}
Robin Davies
  • 7,547
  • 1
  • 35
  • 50