0

I am attempting to save a user id using SharedPreferences. Do values saved as SharePreferences persist across all Activities in my application so I can access the userid from any application? Below is my code for saving the userid.

    userid = result.substring(3, result.length());
                Log.d("userid at onpostexecute", userid);
                SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();  // to update userid
                editor.putString("userid", userid);
                editor.commit();

And here is the code for accessing the userid from the SharedPreferences in another activity.

       SharedPreferences prefs = getPreferences(MODE_PRIVATE);  // to access userid
        String userid = prefs.getString("userid", "");
        Log.d("shared prefs userid", userid);

What is strange is that the above code is in my onCreate method but it doesn't show up in the Logcat even though other log data is displayed before and after this code. So is there something wrong with my code that I can't even get it to display in my logcat? I can't even tell if it is being updated.

Dave
  • 873
  • 2
  • 15
  • 27

2 Answers2

0

I assume you mean can you access them from any Activity, yes, they do persist even when you leave your app and come back. From the Docs

The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).

If this Log.d("userid at onpostexecute", userid); doesn't even show up then I would put a breakpoint there and make sure you have a value for userid. I would also check your logcat filters to make sure that you are getting all logs. Just make sure that the type in the spinner is set to "verbose" just to be sure

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • Thanks for your reply. The userid is now displaying in my logcat. The only reason I can think of as to why it wasn't displaying before was because it was null. – Dave Mar 28 '13 at 03:24
0

Values saved as sharedPreferences can be shared between activities if you tell it to. Right now you are creating a preference that is only accessible to that same activity. You need to use:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();

Like this you are creating a sharedPreference between your application. There is a lot of explanation on the topic in the accepted answer to another question. Link to question As discussed in the answer the way you are using to save the preference will only work in the same activity that saved it.

Community
  • 1
  • 1
Gabriel Netto
  • 1,818
  • 1
  • 16
  • 26
  • Thanks for your reply. I changed it to getDefaultSharedPreferences and it worked. It also was displayed in my logcat. I supposed the reason it wasn't displaying before was because it was null. Stack Overload comes through again. As a newbie to Android I really appreciate your help. – Dave Mar 28 '13 at 03:23