In Activity and Service I create an instance on SharedPreferences like this:
mSharedPreferences = getSharedPreferences(PREFERENCES_NAME, MODE_PRIVATE); PREFERENCES_NAME is a constant.
However, the problem arises when I try to update the data in them (I enter a line in EditText , I write it down): everything is updated in the Activity , but not in the Service , there is old information left! Moreover, after restarting the Service everything starts working!
I write this (in Activity ):
SharedPreferences.Editor editor = mSharedPreferences.edit(); editor.putString(ID_KEY, userIdField.getText().toString()); editor.putString(DOC_URL_KEY, docUrlField.getText().toString()); editor.apply(); And I read like this (in Service ):
mSharedPreferences.getString(DOC_URL_KEY, "") Here is the declaration of classes in the manifest:
<activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".DocumentHandleService" android:process="com.dugin.rostislav.reminderofwork.doc_handling_service" /> What is the problem, why is this happening and how to fix it?
getSharedPreferences(...)withContext.MODE_MULTI_PROCESSflag solve the problem? True it isdeprecated. You can read more here . - post_zeew