Set<String> in android sharedpreferences does not save on force close - android

Im trying to use androids sharedpreferences, I´ve logged everything and the code below really commits the string set. The problem is when I force close the app and start again, the settings.getStringSet returns an empty set. No errormessages anywhere.
I´ve tried PreferenceManager.getDefaultSharedPreferences but that does not work for me either.
Thanks for you time.
public static final String PREFS_NAME = "MyPrefsFile";
private static final String FOLLOWED_ROUTES = "followedRoutes";
and later on when saved is called:
public void onFollowClicked(View view){
SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
Set<String> follows = settings.getStringSet(FOLLOWED_ROUTES, new HashSet<String>());
follows.add(routeId);
editor.putStringSet(FOLLOWED_ROUTES, follows);
editor.commit();
}

You can also work around the bug mentioned by g00dy this way:
Get the set from sharedPreferences and save it in a variable.
Then just delete the set in sharedpreferences before adding it again when saving.
SharedPreferences.Editor editor= sharedPref.edit();
editor.remove("mSet");
editor.apply();
editor.putStringSet("mSet", mSet);
editor.apply();
Make sure to use apply() or commit() twice.
Alternatively, if you are working in Kotlin simply :
PreferenceManager.getDefaultSharedPreferences(applicationContext)
.edit {
this.remove("mSet")
this.apply()
this.putStringSet("mSet", mSet)
}

Take a look here.
Also for refference:
SharedPreferences
SharedPreferences.Editor
EDIT:
There's actually a bug with this one, see here. An extract from there:
This problem is still present on the 17 API level.
It is caused because the getStringSet() method of the
SharedPreferences class doesn't return a copy of the Set object: it
returns the entire object and, when you add new elements to it, the
commitToMemory method of the SharedPrefencesImpl.EditorImpl class see
that the existing value is equal to the previous one stored.
The ways to workaround this issue is to make a copy of the Set
returned by SharedPreferences.getStringSet or force the write to
memory using other preference that always change (for example, a
property that stores the size of the set each time)
EDIT2:
There might be a solution here, take a look.

Related

How to remove Specific values from Shared Preference

Am using SharedPreferences to store list of values. What I need is to remove specific value from SharedPreferences.Below is my code am using to remove. But its not working.
prefs= DetailActivity.this.getSharedPreferences("itemFKID",Context.MODE_PRIVATE);
edit=prefs.edit();
//edit.clear();
edit.remove(itemFkId);
edit.commit();
Below is Screenshot that contains values even after edit.remove() compiles.
Here am inserting values into SharedPreferences
prefs= DetailActivity.this.getSharedPreferences("itemFKID",Context.MODE_PRIVATE);
edit=prefs.edit();
for (int i = 0; i < Config.favouritesList.size(); i++) {
edit.putString("itemFKIDValue" +i, Config.favouritesList.get(i));
}
edit.putInt("itemFKIDLength", Config.favouritesList.size());
edit.commit();
The documentation for SharedPreferences.Editor has two bits that are relevant to your question:
All changes you make in an editor are batched, and not copied back to the original SharedPreferences until you call commit() or apply()
And
when committing back to the preferences, all removals are done first, regardless of whether you called remove before or after put methods on this editor
So you'll have to step over the commit() call before you see the value removed.
Finally found the mistake. Key passed in remove() is wrong. Instead of edit.remove(itemFKIDValue) I have used edit.remove(itemFkID). Thanks for the time guys.

Shared Preference returns always the default value

Here is code I am Using to create and store value in Preference.
outgoing is String variable.
SharedPreferences sp = getSharedPreferences(outgoing, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("PhoneNo","Hi");
editor.commit();
Here is the code to get value from SharedPreference.
SharedPreferences sp
=getSharedPreferences(outgoing,Activity.MODE_PRIVATE);
String calln = sp.getString("PhoneNo","0");
Toast.makeText(mContext, "SHARED"+calln,Toast.LENGTH_LONG).show();
You should probably call the getSharedPreferences on the context from which you are accessing them.
Source
Therefore, depending on how you can access your context, if you pass it to some other activity or in an asynchronous task, here are some examples of usage:
this.getSharedPreferences(outgoing, Activity.MODE_PRIVATE);
context.getSharedPreferences(outgoing, Activity.MODE_PRIVATE);
getApplicationContext().getSharedPreferences(outgoing, Activity.MODE_PRIVATE);
Also, one way you can test your stuff is to use a listener when SharedPreferences get changed:
onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
Called when a shared preference is changed, added, or removed.
here is how to do that
You can also use the Preference Manager to obtain the SharedPreferences:
PreferenceManager.getSharedPreferences(YOUR_CONTEXT).getString(
"PhoneNo", "0");
Or to store them:
PreferenceManager.getSharedPreferences(YOUR_CONTEXT).edit().putString(
"PhoneNo", "Hi").commit();
The most likely reason that Shared Preference always returns the default value is that you saved the value in one preference file and then tried to retrieve it in another preference file. This can happen if you do call getPreferences() from different Activities, because getPreferences() creates a different preference file based on the activity it is created in.
Solution
The easiest solution is to always get your shared preferences like this:
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
This will use a single preference file for the entire app.
Alternate solution
If for some reason you need to use different preference files, then you can do
final static String PREF_FILE_1 = "pref_file_1";
...
SharedPreferences sharedPref = context.getSharedPreferences(PREF_FILE_1, Context.MODE_PRIVATE);
Just make sure you always use the right file name for the preferences you are trying to save and retrieve.
Local preferences
If you really only need a preference for a specific Activity, then you can use getPreferences(Context.MODE_PRIVATE). Just don't expect to be able to retrieve the values from another Activity in the same way.
See also
This answer describes the differences between the various ways of obtaining SharedPreferences.
Difference between getDefaultSharedPreferences and getSharedPreferences
Mess with the shared preferences of android - which function to use?
change this Activity.MODE_PRIVATE to this Activity.MODE_MULTI_PROCESS, issue is probably due to different context during storing value and accessing value.
When putting values, try changing this:
SharedPreferences sp = getSharedPreferences(outgoing, Activity.MODE_PRIVATE);
to this:
SharedPreferences sp = getApplicationContext().getSharedPreferences(outgoing, Activity.MODE_PRIVATE);
Same when getting values - don't forget to add getApplicationContext() in your call to SharedPreferences
EDIT:
Check that your "outgoing" String is the exact same in both Activities

SharedPreferences value is not updated

I am trying to update the values of SharedPreferences, here is my code:
edit = PreferenceManager.getDefaultSharedPreferences(this).edit();
edit.putString(Settings.PREF_USERNAME+"",txtuser);
edit.putString(Settings.PREF_PASSWORD+"",txtpass);
edit.commit();"
The problem is that when I am accessing this values, it is not returning updated values, it gives me a value of SharedPreferences.
But when I am confirming the data in XML file ,the data updated in that.
And after restarting my application I am getting that updated values. So it requires me to restart the application to get updated values.
So, how to get those updated values once it changes?
Thanks in advance
Here is my whole code:
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
ctx=this;
status=PreferenceManager.getDefaultSharedPreferences(this).getString(Settings.PREF_STATUS, Settings.DEFAULT_STATUS);// get old value
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
on(ctx,true);// function will call and value is updated
}
}});
status=PreferenceManager.getDefaultSharedPreferences(this).getString(Settings.PREF_STATUS, Settings.DEFAULT_STATUS);// this should give me a updated value but gives old value
}
public static boolean on(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(Settings.PREF_ON, Settings.DEFAULT_ON);
}
public static void on(Context context,boolean on) {
if (on) Receiver.engine(context).isRegistered(); //
}
**********in reciver file***********
public void isRegistered ) {
Editor edit = PreferenceManager.getDefaultSharedPreferences(Receiver.mContext).edit();
edit.putString(Settings.PREF_STATUS+"","0");
edit.commit();
}
Instead of using edit.commit();, you should use edit.apply();. Apply will update the preference object instantly and will save the new values asynchronously, so allowing you to read the latest values.
commit()
Commit your preferences changes back from this Editor to the
SharedPreferences object it is editing. This atomically performs the
requested modifications, replacing whatever is currently in the
SharedPreferences.
Note that when two editors are modifying preferences at the same time,
the last one to call commit wins.
If you don't care about the return value and you're using this from
your application's main thread, consider using apply() instead.
apply()
Unlike commit(), which writes its preferences out to persistent
storage synchronously, apply() commits its changes to the in-memory
SharedPreferences immediately but starts an asynchronous commit to
disk and you won't be notified of any failures. If another editor on
this SharedPreferences does a regular commit() while a apply() is
still outstanding, the commit() will block until all async commits are
completed as well as the commit itself.
As SharedPreferences instances are singletons within a process, it's
safe to replace any instance of commit() with apply() if you were
already ignoring the return value.
You don't need to worry about Android component lifecycles and their
interaction with apply() writing to disk. The framework makes sure
in-flight disk writes from apply() complete before switching states.
Well, even if my answer came 3 years after the question, I hope it will help. The problem don't seem to came from commit or apply but from the code structure.
Let's explain: on a smartphone, you run an APP but you don't quit the APP as we do on computers.
This mean when you come back to the menu of the smartphone, the APP is still "running". When you "click" again on the APP icon, you don't re-run the APP but just awake it.
In juned code, we can see he calls getDefaultSharedPreferences inside his Create function.
So he calls getDefaultSharedPreferences when he runs first time the APP. But when he sets the APP on background and then awakes the APP, the call is not done.
I've had the same problem:
I check if I have SharedPreference for my APP. If not, I prompt a form to ask value to the user.
If yes, I check the date of the preferences. If too old, I prompt the form.
After the form, I save the preferences with the current date.
What I noticed is that the test about the existence of the SharedPreference (which was set at the same location than the one of juned) was done only at first run of the APP but not when I awake the APP. This mean that I was unable to check the time limite of my SharedPreferences!
How to solve that?
Just add:
#Override
public void onResume(){
super.onResume();
// And put the SharedPreferences test here
}
This code will be called at first run of the APP but also each time the user awake it.
hope it will help you..
SharedPreferences mypref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor prefsEditr = mypref.edit();
prefsEditr.putString("Userid", UserId);
prefsEditr.commit();
String task1 = mypref.getString("Userid", "");
Try this code:
SharedPreferences edit = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor1 = edit.edit();
editor.putString(Settings.PREF_USERNAME + "", txtuser);
editor.putString(Settings.PREF_PASSWORD + "", entered_name);
editor.commit();
Try like this,
public SharedPreferences prefs;
SharedPreferences.Editor editor = prefs.edit();
editor.putString(Settings.PREF_USERNAME+"", txtuser);
editor.putString(Settings.PREF_PASSWORD+"", entered_name);
editor.apply()
Try like this,
public SharedPreferences prefs;
SharedPreferences.Editor editor = prefs.edit();
editor.putString(Settings.PREF_USERNAME+"", txtuser);
editor.putString(Settings.PREF_PASSWORD+"", entered_name);
editor.commit();

getSharedPreferences not returning a value

I've placed a preferences file 'xml/sleeppreferences.xml' into a tabSpec, with the idea that that is the preferences for that part of the app and there will be other preference files for other parts.
This seems to work ok. I make a preference change, close the emulator, re-run the app, go back to the preference page, and the preference is what I had set it to.
But when I click to another tab, where I want to use the value of that preference, it all goes wrong.
I've looked high and low, but cannot find an answer.
This is an excerpt of the code:
public static final String PREF_FILE_NAME = "sleeppreferences";
:
:
:
SharedPreferences prefs = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
String test=prefs.getString("editTextPref", "unset");
with "unset" being the default response if it doesn't find anything.
It always returns "unset"
As I say, I've looked all over, and the code I'm using seems to be the correct code. So what's going on?
Thanks
Dave
Try using the GetSharedPreferences of the context class. Something like this:
public String GetPassword (Context Contexto, String Key) throws Exception
{
SharedPreferences savedSession= Contexto.getSharedPreferences(Key,Context.MODE_PRIVATE);
return Encryption.decrypt(_Seed,savedSession.getString(Key, null));
}
I think this should work as well:
getApplicationContext().getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE).getString("editTextPref", "unset");
Please, try this code to save your preferences and then use the code I previously pasted. It has to work fine.
SharedPreferences savedSession= context.getSharedPreferences(Key,Context.MODE_PRIVATE);
Editor editor = savedSession.edit();
editor.putString(Key, EncryptedPass);
editor.commit();

Service not saving SharedPreference for Main Activity?

I am using a service to download and retrieve a list of URL's and put them in a sharedpreference.
With this..
SharedPreference images_article = this.getSharedPreferences("images_articles", MODE_WORLD_READABLE);
editor.putString("article2", urlImage2);
editor.putString("article3", urlImage2);
editor.commit();
Then in my Main.Activity i pull the url's from preference.
SharedPreference images_article = this.getSharedPreferences("images_articles", MODE_WORLD_READABLE);
urlImage2 = images_article.getString("article2", "NO ARTICLE AVAILABLE");
urlImage3 = images_article.getString("article3", "NO ARTICLE URL AVAILABLE");
The only problem is for some reason it isnt going inside of the shared preference, because the Main activity is loading the OLD URL's that has now changed. But in the Service i log the url's being retrieved and they are updated but for some reason in the main activity it still loads the old one. and im retrieving them from the same preference.
Is there anything i am missing or a better way to do this? Any help would be great!!
I was running in to a similar issue with my SharedPreferences between my Activity and Service. I ended up not using the default and used my own set file name
in the activity and service I set
private static final String PREFERENCE_NAME = "MyPreferenceFileName";
Then to get the values:
SharedPreferences pref = getSharedPreferences(PREFERENCE_NAME, Activity.MODE_PRIVATE);
pref_checked = pref.getBoolean("checked", true);
and to set the values:
SharedPreferences pref = getSharedPreferences(PREFERENCE_NAME, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("checked", value);
editor.commit();
This allowed me to use the same get and put logic in both my Service and Activity without any issues. I hope this helped.
This might be happening due to the different contexts you are accessing from. I am not very sure though, but you can try this :
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sp.edit();
Use this code, whenever you try to access SharedPreferences, that is, from both the Service and the Activity. That might solve your problem.
Using this in both the Activity and Service classes seems to work for me, as it should now be using the same Context to access the application SharedPreferences:
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

Categories

Resources