Default Shared Preferences give me wrong values in Service
android, sharedpreferences
Solution
I found a solution thanks to this link Managing SharedPreferences in both PreferenceActivity and Service within same app and thanks to Andrew T. :
The issue was the multi process mode. If you have a Service which is declared in the manifest with a android:process="" (which is my case) then it's necessary to set a multi process mode.
Here is what i did :
in PreferenceFragment :
public static final String CONFIG_NAME = "pref";
...
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getPreferenceManager().setSharedPreferencesName(CONFIG_NAME);
getPreferenceManager().setSharedPreferencesMode(Context.MODE_MULTI_PROCESS);
addPreferencesFromResource(R.xml.config);
...
}
My Service :
SharedPreferences settings = MyApplication.getInstance().getSharedPreferences(OptionsFragment.CONFIG_NAME, Context.MODE_MULTI_PROCESS);
and I set the default values in MainActivity this way :
PreferenceManager.setDefaultValues(getApplicationContext(), OptionsFragment.CONFIG_NAME, Context.MODE_MULTI_PROCESS, R.xml.config, false);
And now it works fine. Hope it will help !
Thank you again Andrew T.
Problem
I have a PreferenceFragment where I have defined a CheckBoxPreference in XML. I need to check this value in a Service, but it always gives me the old value. I noticed the value is correctly changed when I restart the application. My Preference Fragment : ``` public class OptionsFragment extends PreferenceFragment { public static final String WIFI_ONLY = "wifi"; private SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MyApplication.getInstance()); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.config); } } ``` My config.xml : ``` <?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <CheckBoxPreference android:defaultValue="true" android:key="wifi" android:summary="Check if you want to use wifi only" android:title="Use Wifi only" /> </PreferenceScreen> ``` My Service : ``` SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MyApplication.getInstance()); Log.d(TAG, "isWifiOnly : "+settings.getBoolean(OptionsFragment.WIFI_ONLY, true)); ``` The log always return the same value no matter if I change it or not, except if i restart the app. Also, in my MainActivity I have that line in OnCreate(): ``` PreferenceManager.setDefaultValues(getApplicationContext(), R.xml.config, false); ``` It creates the config file with the default value if needed. I'm doing something wrong, the question is what ?