Not Getting Integer Value From SharedPreference
I am trying to store user id in SharedPreference in one activity and want to get this integer id in any activity. To put this value in Shared Preference i use following code. Sh
Solution 1:
There is nothing wrong with your code and I tested this exact case and it is working perfectly. For sure you can use the default value as -1.
Do you have both activities in the same application?
What I can suggest for debugging is to make sure that your application runs in the sequence you expect and that the value is stored correctly. You can try to retrieve it directly after storing it within the same activity.
Solution 2:
Change this:
SharedPreferences myPrefs = getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
int userId = myPrefs.getInt("userId", -1);
to this
SharedPreferences myPrefs = getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
int userId = myPrefs.getInt("userId", 0);
it should work that way.
Post a Comment for "Not Getting Integer Value From SharedPreference"