Fix: SharedPreferences not saved after app restart

21 09 2012

It is quite common error that SharedPreference setting is being reset or cleared to default, after Android application is restarted. There are two things you have to remember.

Commit changes made by prefs editor (the obvious one)


Clear prefs editor before using it (the tricky one)


The clear() method clears your preferences made by editor. Clear() is also always called in commit() method (so after the change in editor was made).


So to solve this problem You need to write code like this (compare it with snippets in previous post):

public static void setPushEnabledFlag(Context context, boolean newValue) {
	SharedPreferences prefs = context.getSharedPreferences(Constants.SP_MY_APPLICATION_SETTINGS, 0);
	Editor prefsEditor = prefs.edit();

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!




14 responses

4 12 2012

Awesome! You saved and made my day!!

14 01 2013
angel zambrano

Gracias, después de horas y horas una respuesta muy util.

8 08 2013

Hey i have a problem when i make some change in EditTextPreferences
it does not apply on my application until i restart this application kindly help me how can i apply change on my application by going back on it.

30 10 2013

Finally got the solution! You saved my day dude!
There’s no one mentioned should try clear()…

30 10 2013
Jacek Milewski

It took me some time to figure out that clear() is needed. I’m glad I could help and save your time :)

10 04 2015
Chris Cha

This worked for me thanks!

10 05 2015

Thanks! But I have a question? If you need put two value (ex: int x, string y) into SharedPreferences with two different method. Fist you clear() and putInt(x) in method one, next clear() and putString(y) in method two. the value of int x will be lost?

PS: sorry for my bad English.

10 05 2015
Jacek Milewski

Dont know. Try it

10 06 2015

i dont get when to use clear and when to not
when i force close my application or reboot device all the data is removed
basically i was working on project in which sign in login in etc etc but when user sign in and force close then application user have to again sign in

24 06 2015

Finaly made my app works thanks to this.

22 04 2016
Tomaž Ravljen

Thanks man! How is this not like everywhere? I kept losing data after app restart. Since I was updating preferences in Application class, IntentService and in different activities… And I had no idea what It lost data, still don’t. But at least now it works! :)

12 02 2018
amit pandya

Hi, I want to store my model object to retrieve values from that, when I am trying to do that, it’s getting saved, any help would be amazingly appreciated. thanks in advance.

16 02 2018

Thank you!!! I put a lot of effort in order to solve it and you gave me the clearest answer!! good job

22 04 2018

This solution is wrong. clear() removes ALL preferences.
The correct solution can be found here

Give Your feedback:

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: