Strahinja Ajvaz
Strahinja Ajvaz

Reputation: 2633

SharedPreferences is not updating its value

Below is a snippet of code that i use in my save settings function (DialogFragment):

String orderBy = mOrderBySpinner.getSelectedItem().toString();
String search = mSearchEditText.getText().toString().trim();

SharedPreferences sharedPreferences = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();

editor.putString(InventoryPreferences.ORDER_BY, orderBy);
editor.putString(InventoryPreferences.SEARCH_TERM, search);
editor.apply();

I then retrieve that data with the following (Activity):

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
String orderBy = sharedPrefs.getString(InventoryPreferences.ORDER_BY, "name ASC");
String searchTerm = sharedPrefs.getString(
                InventoryPreferences.SEARCH_TERM,"").trim();

These are my keys:

public static final String ORDER_BY = "orderBy";
public static final String SEARCH_TERM = "search";

Is there any reason as to why it wouldn't update the values when the key is the same?

Upvotes: 0

Views: 839

Answers (4)

skeidsberget2365
skeidsberget2365

Reputation: 173

You are using two different methods to get access to a SharedPreferences file.

The first time using getActivity().getPreferences(Context.MODE_PRIVATE) you are calling Activity's getPreferences(int mode) which returns a SharedPreferences object which is supposed to be private to the activity which requests it. The name of the preferences file this SharedPreferences object points to is CLASS_NAME.xml

The second time using PreferenceManager.getDefaultSharedPreferences(this) returns a SharedPreferences object which is supposed to be available and useful for the entire application. The name of the preferences file this SharedPreferences object points to is PACKAGE_NAME_preferences.xml.

So your problem is that you are using one file to write the preferences to and another to read them. Try using the more global thinking PreferenceManager.getDefaultSharedPreferences(Context context) to store preferences that relate to the entire application, and only use Activity.getPreferences(int mode) for preferences that only relate to a specific activity. (And then also remember to use the appropriate methods to retrieve them)

Upvotes: 1

BAnd
BAnd

Reputation: 1

Your code for updating preference value is correct.

Consider verifying that the values in input controls really changed and verify, that the application has permission to write preferences.

Upvotes: -1

W4R10CK
W4R10CK

Reputation: 5550

Use PreferenceManager to access SharedPreference.

Upvotes: 0

marmor
marmor

Reputation: 28179

getActivity().getPreferences(Context.MODE_PRIVATE);

is not

PreferenceManager.getDefaultSharedPreferences(this);

Use the second line in both methods.

From the docs: https://developer.android.com/reference/android/app/Activity.html#getPreferences(int)

Retrieve a SharedPreferences object for accessing preferences that are private to this activity

Upvotes: 2

Related Questions