6

I backup the SharedPreferences xml file to the SD card, from the file

File taskExceptionFile = new File(Environment.getDataDirectory()
                + "/data/" + packageName + "/shared_prefs/" + TaskAction.SHARED_PREF_TITLE_TASK_EXCEPTION + ".xml");

however, when I restore the file, if the file has been open by

SharedPreferences sp = context.getSharedPreferences(TaskAction.SHARED_PREF_TITLE_TASK_EXCEPTION, 0);

before restoring, the SharedPreferences seems to have a cache, I use Root Explorer to see the file after restoring, the values are indeed the values from the backup file, but when I use

sp.getInt(TaskAction.SHARED_PREF_TASK, 0)

to get the value, the value is not the new value from the backup file, it returns the default value 0. I force stop my app, and reenter my app, the values are the new values.

So, I want to ask how to refresh the SharedPreferences immediately when I restore the file, so that I can get the new value?

Sorry for my poor English~~~

Xiaochen Wang
  • 83
  • 1
  • 7
  • Are you calling the SharedPreferences from a thread? I came across this problem once. Check my answer here. http://stackoverflow.com/questions/12630926/sharedprefences-not-being-updated – DeeV Jan 21 '13 at 16:22
  • Why do you save preferences to file on SD card, when actually they are normally stored in more secure location in inner phone's memory? – Kamil Lelonek Jan 21 '13 at 16:26
  • Thanks for your help, @DeeV. I tried your answer, and the problem is solved. [Answer](http://stackoverflow.com/a/12644472/1393699) – Xiaochen Wang Jan 22 '13 at 05:59
  • 1
    In my app, I save some settings in the preferences, and the users want to backup their settings without the network, so I backup the preferences and databases on SD card. @squixy – Xiaochen Wang Jan 22 '13 at 06:06
  • Hello did you get the solution. I am stuck with same problem. Please help – piyush poriya Sep 24 '16 at 10:51

0 Answers0