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);
Related
how can I edit value of sharedPreference from other Activity. I try this codes by I'm getting an error on the context part.
if(stars == 2){
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = scorepref.edit();
editor.putInt("keyhelloworld", stars);
editor.commit();
Intent fromHW = new Intent(HelloWorldGameActivity.this, LessonActivity.class);
startActivity(fromHW);
}
try this.
Pass context from first activity.
To call Shared preferences
SharedPreferences sharedPreferences = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
To call editor
SharedPreferences.Editor editor = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE).edit();
You can use SharedPreferences as follows. Since sharedpreferences is persistent, you can use the same implementation anywhere in your application to access it.
SharedPreference sharedPreferences = getApplicationContext().getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
sharedPreferences.edit().putString("key", value).apply();
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've on Activity A the following code:
SharedPreferences sharedPreferences = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("nome", nome.getText().toString());
editor.commit();
I've on Activity B the following code:
SharedPreferences sharedPreferences = getSharedPreferences("prefs", 0);
String a = sharedPreferences.getString("nome", "");
nomeMediador.setText(a); //TextBox
Can anyone tell why it's not showing (the value saved on Activity A) on Activity B?
Refer the below code
Activity A
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("nome", nome.getText().toString());
editor.commit();
Activity B
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
String a = sharedPreferences.getString("nome", "");
nomeMediador.setText(a);
its better use getter and setter with the help of string.xml file, you will never get this kind of problem. for that matter you may check this blog:
http://sspower3.blogspot.in/2011/11/sharedpreferences-in-eazy-way.html
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'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();