I am using Shared Preferences in my Android application.
It works fine, but when I restart the application, all the shared preferences values are gone.
Why?
I am using Shared Preferences in my Android application.
It works fine, but when I restart the application, all the shared preferences values are gone.
Why?
Without code is diffucult to resolve. Anyway, i suppose you don't reinstall every time application before launch it. So probably you don't commit changes to shared preference. From Saving Key-Value Sets:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();
Check your code to verify presence of commit()
instruction.
1st are you doing more things in the if statement? Because you are creating two variables that do nothing and get destroyed after this clause 2nd do you even assign "text" preference at all when not it is not loading because you get the default null back
I'm sorry I thought it was your code but then it goes to assad
I think you are not commiting those changes. SharedPreferences.Editor.commit() is must applied after putting the values. Commit ensures the values has been saved.
Consider this the accepted answer :
I don't know why, but it is working by just putting your prefs code inside the async task:
prefss = getSharedPreferences(ACCOUNT_PREFS_NAME, MODE_MULTI_PROCESS);
new AsyncSave(favNamesList).execute();
private static class AsyncSave extends AsyncTask<Void, Void, Boolean> {
String favNamesList;
AsyncSave(String favNamesList) {
this.favNamesList = favNamesList;
}
@Override
protected Boolean doInBackground(Void... params) {
prefss.edit().putString("favNamesList", strings).apply();
return null;
}
}