Search code examples
androidandroid-preferencesandroid-settings

Why is this preference not being saved?


As far as I can tell, I am using the same key when getting as when setting, yet the preference does not save. I'm sure I'm missing something obvious.

My custom DialogPreference where I load the current preference and set the new one.

public class SemesterDialogPreference extends DialogPreference {

    View view;
    SharedPreferences settings;
    String semester_id_key = this.getContext().getApplicationContext().getString(R.string.pref_semester_id);

    DBAdapter db;

    @Override
    public void onBindDialogView(View view){
            this.view = view;
            settings = getSharedPreferences();

            int curSemesterId = settings.getInt(semester_id_key, 1);
            System.out.println("Incoming " + semester_id_key + " equals " + curSemesterId);

            // Here I do stuff...

            super.onBindDialogView(view);
    }


    public SemesterDialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        setDialogLayoutResource(R.layout.dialog_set_semester);

    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);

        // Do things and set newId


        System.out.println("new int for " + semester_id_key + " is " + newId);
        Editor editor = getEditor();
        editor.putInt(semester_id_key, newId);
        editor.commit();
    }
}

The XML for my PreferenceScreen

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <PreferenceCategory android:title="@string/pref_header_settings" >
            <com.NsouthProductions.gradetrackerpro.SemesterDialogPreference
                    android:title="@string/pref_title_semester"
                    android:summary="@string/pref_summary_semester"

                    android:key="@string/pref_semester_id"
                    android:dialogMessage="Are you sure?"
                    android:positiveButtonText="Clear Questss"
                    android:negativeButtonText="Cancel"/>

    </PreferenceCategory>

</PreferenceScreen>

Also, I don't understand why examples online don't include parameters in getSharedPreferences(). I thought that's how android knew which file to use.


Solution

  • Here's how to write to SharedPreferences:

        public static final String PREFS_NAME   = "UNIQUE-NAME"; // settings will be saved here 
    
        // ...       
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        Editor editor = settings.edit();
        editor.putString("abc", "123");
        editor.commit();
    

    In case you get an error on:

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    

    you can do:

    SharedPreferences settings = getActivity().getPreferences(PREFS_NAME, 0);
    

    and if you're trying to approach it from a different activity, then do:

    SharedPreferences settings = getSharedPreferences(YourBaseActivity.PREFS_NAME, 0);
    

    (needless to say that you should replace YourBaseActivity with the name of the activity in which you declared the SharedPreferences to begin with).