Can not save changes into SharedPreferences - android

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();

Related

BackgroundImage is not setting properly in SharedPreferences

I am trying to save the backgroundImage of the ViewPager, using SharedPreferences and load that image as background when we run the app for the next time. It works fine in the emulator but not in the device. Please help me in solving this.Thanks,
Here is my code..
SlidePagerActivity.mPager.setBackgroundResource(R.drawable.snow);
SharedPreferences backgroundImage = getSharedPreferences("BGPREFS", MODE_WORLD_READABLE);
SharedPreferences.Editor bgEditor = backgroundImage.edit();
bgEditor.putInt("imgValue", R.drawable.app_bg_snowfall);
bgEditor.commit();
Try this:
To put in SharedPreferences
SharedPreferences sp = getSharedPreferences("myApp", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("NewBackground", idImage);
editor.commit();
To get the Preference:
SharedPreferences sp = getSharedPreferences("myApp", Activity.MODE_PRIVATE);
int myNewIntValue = sp.getInt("NewBackground", 0);
and finally use it. MyNewIntValue

Shared Preferences in not being cleared,

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);

Android Shared Preferences betwen Activities

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

How do I change the value of a SharedPreference without going into the preference screen?

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!

how to empty the sharedpreferences storage in android?

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();

Categories

Resources