Reputation: 96
Ok i'm following an android book and they are adding settings to a sudoku game using a class that extends PreferenceActivity, this class is called by an intent and all it does is addPreferencesFromResource(R.xml.settings), this approach has been deprecated and its not working anymore, here is the code from the book:
package org.example.sudoku;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class Prefs extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settings);
}
}
Now i have looked into this matter and found that you have to instance a PreferenceManager in order to do this, but in the example i found they extends the Prefs class from PreferenceFragment (not PreferenceActivity as in the book), i managed to work on the code as follows:
/*
* this is for use from API version 11 and after...
*
*/
package org.example.sudoku;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
public class Prefs extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Make sure default values are applied. In a real app, you would
// want this in a shared function that is used to retrieve the
// SharedPreferences wherever they are needed.
PreferenceManager.setDefaultValues(getActivity(),
R.xml.settings, false);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settings);
}
}
But this doesn´t do the job, i don´t know if its because i am calling this class from an intent and this class extends PreferenceFragment instead of PreferenceActivy or this is not the way of doing this thing, can someone help me out to understand this?
Upvotes: 1
Views: 1023
Reputation: 16354
final String PREF_SETTINGS_FILE_NAME = "PrefSettingsFile";
To read the values of your preference variables.
SharedPreferences preferences = getSharedPreferences(PREF_SETTINGS_FILE_NAME, MODE_PRIVATE);
prefSettingsValue1= preferences.getInt("value1", 1); // default value of prefSettingsValue1 will be 1 in case you are trying to read a non-existent value. You can specify it according to your wish. (like I have done for the next value)
prefSettingsValue2= preferences.getInt("value2", 0);
prefSettingsValue3= preferences.getInt("value3", 1);
To write values into your preference variables.
SharedPreferences preferences = getSharedPreferences(PREF_SETTINGS_FILE_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("vaule1", prefSettingsValue1);
editor.putInt("value2",prefSettingsValue2);
editor.putInt("value3",prefSettingsValue3);
editor.commit();
Note : No need to use any XML file to store the preferences. Just save the values in the Preference variables and read them later in you application. Not just integers, you can also get
and put
Strings by using getString
and putString
functions to read and write, respectively. Also, you do not need to extend any Preference Activity/Fragment.
Upvotes: 2