7

I have two activities A and B. Activity A has a form and by clicking on submit button it saves the form data into shared preference. Activity B has to retrieve data from the shared preferences saved by activity A. I went through so many forums and got suggestion to uses getSharedPreference(Filename,Mode) instead of getPreference(Mode). But still it does not work. Is there any other method to do this job instead of creating a class in activity A specially to extract shared preference values for public use.

Please suggest a perfect solution for this. I even used the mode as WORLD_READABLE.

Surjya Narayana Padhi
  • 7,741
  • 25
  • 81
  • 130
  • please post the code that you are saving your SharedPreferences with. – Jon Willis Sep 17 '11 at 15:20
  • Please check my code here - http://pastebin.com/0W64gnEt. Please search for the word "XYZ12345" in my code where I have written my problem as comment. I would be obliged to get the solution. Thanks – Surjya Narayana Padhi Sep 17 '11 at 15:35

3 Answers3

3

I think the problem is that the editor is not actually committing changes due to how you are opening your SharedPreferences.

 SharedPreferences.Editor editor = getSharedPreferences("udata",MODE_WORLD_READABLE).edit();

Change MODE_WORLD_READABLE to Context.MODE_PRIVATE and see if that helps.

Jon Willis
  • 6,993
  • 4
  • 43
  • 51
  • servState = 1; editor.putInt("AppState", servState); editor.commit(); I have committed the changes in the above code. Initially I was using the MODE_PRIVATE only but since it din't work I used the MODE_WORLD_READABLE mode. any other suggestion please – Surjya Narayana Padhi Sep 17 '11 at 16:09
  • You should debug the commit code, ensure that mPwd.getText().toString() is not returning null, and ensure that the commit is working. – Jon Willis Sep 17 '11 at 16:22
  • i have same problem, when i start activity when back onresume() is call " pref.getBoolean(Constant.PREF_KEY, true) " or startactivity(...); finish(); – vuhung3990 Mar 27 '14 at 10:01
0

You can try using PreferenceManager.getDefaultSharedPreferences. The JavaDoc is here: http://developer.android.com/reference/android/preference/PreferenceManager.html

Your other attempts should work, but without seeing the code I'm not sure why it is isn't.

Eric Levine
  • 13,536
  • 5
  • 49
  • 49
0

There was some initialization problem. Now I got the solution. Thanks for all your support

Surjya Narayana Padhi
  • 7,741
  • 25
  • 81
  • 130