i'm using sharedPreferences to store username and password .. but i need to make log off, how can i remove the data od username and password ?
You have to use the SharedPreferences Editor object to do it. You can follow the next example:
SharedPreferences settings = getSharedPreferences("settings", MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.remove("username_field");
editor.remove("password_field");
editor.commit();
Just in case you want to clear the entire preference too, useful if you've only got the users details in the preference, and you want to remove all of that, you Can use.
SharedPreferences preferences =
getSharedPreferences("PREFERENCE",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.commit();
Related
I have a few data I saved in SharedPreference. WHen a button is clicked, the user is logged out and the sharedPreference is cleared by calling pref.clear() ad pref.commit(). When a user tries to log back in, the editor is called to commit() but it wouldn't save the new values. Any suggestions why shared preference is not saving after clearing is done?
Clearing part:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor sa = prefs.edit();
sa.clear();
sa.commit();
re-adding part:
editor.putString("bucket", bucket);
editor.putString("profileid", profileid);
editor.putString("username", user);
editor.putString("password", pass);
editor.commit();
CLOSED: answer in the bottom but for anyone else that is looking for an answer, i accepted the answer
how are you initializing editor (whitch I asume is SharedPreferences.Editor). I use the following to get editor.
SharedPreferences settings = getActivity().getSharedPreferences("a_string", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
I do this the same way for clearing and saving my data.
pleas post more information if this dose not help.
Actually my fault, there was a typo while I was initiation my preferences. Sorry folks!
Let me show you a working example of how to add/read/remove data from SharedPeferences.In your Activity for adding some data into SharedPreferences
SharedPreferences prefs = getApplicationContext().getSharedPreferences("yourPrefName",MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("bucket", bucket);
editor.putString("profileid", profileid);
editor.putString("username", user);
editor.putString("password", pass);
editor.commit();
You would want to do the above when is user is logged in successfully.Now when the user logout, inside your onClickListener for logout Button
SharedPreferences settings = getApplicationContext().getSharedPreferences("yourPrefName",MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.clear();
editor.commit();
So in your Activity's code you should check if SharedPreferences values are already set.If they are set, then you can redirect user to the home screen without having to login again.
I want to save some data into SharedPreferences. everything is intact. I can read from it but I can not save anything.
SharedPreferences prefs = getSharedPreferences("preferences", 0);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("age", 25);
editor.putString("name", "name1");
editor.commit();
I have checked everything many times. any suggestion ?
EDIT: I solved it by using this line:
SharedPreferences Prefs = PreferenceManager
.getDefaultSharedPreferences(this);
You're going to want to add .apply(); after any change. So for example the
editor.putInt("age", 25);
would change to
editor.putInt("age", 25).apply();
I'm unable to delete SharedPreferences from the app on click event.
Here is how I'm storing the value into UserInfoActivity SharedPreferences:
SharedPreferences notificationCountSP = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor notificationEditor = perkBalance.edit();
notificationEditor.putString("notificationCount",notificationCountValue);
notificationEditor.commit();
And here is how I'm trying to clear all data in SharedPreferences from MainActivity:
SharedPreferences clearNotificationSP = getSharedPreferences(
"notificationCountSP", 0);
SharedPreferences.Editor editor = clearNotificationSP.edit();
editor.remove("notificationCount");
editor.clear();
editor.commit();
Please tell what am I doing wrong with this.
Any kind of help will be appreciated.
SharedPreferences notificationCountSP = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor notificationEditor = notificationCountSP.edit();
notificationEditor.putString("notificationCount", notificationCountValue);
notificationEditor.commit();
notificationEditor.remove("notificationCount");
notificationEditor.commit();
or
SharedPreferences clearNotificationSP = getSharedPreferences("notification_prefs", 0);
SharedPreferences.Editor editor = clearNotificationSP.edit();
editor.putString("notificationCount", notificationCountValue);
editor.commit();
editor.remove("notificationCount");
editor.commit();
First solution uses the default application preferences file, and the second a custom notification_prefs file.
You are using PreferenceManager.getDefaultSharedPreferences to store but retrieving from getSharedPreferences("notificationCountSP"). They are different files unless you set the default one to "notificationCountSP".
You can do it like below
SharedPreferences userPref = getSharedPreferences(
MyActivity.SHARED_PREFERENCES_FILENAME,MODE_PRIVATE);
I have a string I want to store in my SharedPreferences. Is there some kind of setString I could do to accomplish this?
You mean something like this?
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("username", "myUsername");
editor.commit();
You can make changes in the SharedPreferences, and commit the changes after you are done.
Try this.
SharedPreferences prefs = getSharedPreferences("PreferenceFileName", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("StringNameToBeStored", "value");
editor.commit();
And you can do everything in the same line, there's no need to declare editor
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
sharedPreferences.edit().putString("username", "myUsername").commit();
And don't forget the commit!
I check the SharedPreferences example and curious about the code for data modification in SharedPreferences:
SharedPreferences preferences = getSharedPreferences (name, MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Greeting", "Welcome to sharedpreferences!");
editor.commit();
Log.d("shared preferences", preferences.getAll().toString());
I wonder why the lines of second to fourth:
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Greeting", "Welcome to sharedpreferences!");
editor.commit();
can't rewrite as:
preferences.edit().putString("Greeting", "Welcome to sharedpreferences!");
preferences.edit().commit();
LogCat does not show up any key pair values after this change. It seems not feasible to write with this way. Just wonder why it necessary to declare an SharedPreferences.Editor object rather than directly called from the SharedPreferences class?
The source code of SharedPreferences:
http://www.java2s.com/Open-Source/Android/UnTagged/dexandroid/android/content/SharedPreferences.java.htm
The link you gave is for documentation and interface, not the actual implementation of the SharedPreferences you got.
You are not promised to get the same editor each time you call edit(), so calling commit on editor will not commit the changes in the object preferences.edit() since it might be a separate object.
In your example:
SharedPreferences.Editor editor = preferences.edit();
// ^object #1
editor.putString("Greeting", "Welcome to sharedpreferences!");
//^object #1
editor.commit();
//^object #1
preferences.edit().putString("Greeting", "Welcome to sharedpreferences!");
// ^object #2
editor.commit();
//^object #1
You can rewrite it as:
SharedPreferences preferences = getSharedPreferences(name, MODE_PRIVATE);
preferences.edit()
.putString("Greeting", "Welcome to sharedpreferences!")
.commit();
Log.d("shared preferences", preferences.getAll().toString());