Questions tagged [sharedpreferences]

SharedPreferences allows you to save and retrieve persistent key-value pairs of primitive data types in Android applications. It can be used either inside a single app or shared between multiple apps.

SharedPreferences allows you to save and retrieve persistent key-value pairs of primitive data types in Android applications. It can be used either inside a single app or shared between multiple apps.

You can learn more about SharedPreferences under the Data Storage section in the Android documentation.

7696 questions
71
votes
3 answers

How to save and retrieve Date in SharedPreferences

I need to save a few dates in SharedPreferences in android and retrieve it. I am building reminder app using AlarmManager and I need to save list of future dates. It must be able to retrieve as milliseconds. First I thought to calculate time between…
Isuru Madusanka
  • 1,397
  • 4
  • 19
  • 27
63
votes
2 answers

Android getDefaultSharedPreferences

My code is: final String eulaKey = "mykey"; final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); boolean hasBeenShown = prefs.getBoolean(eulaKey, false); Always returns different values depending…
Kostadin
  • 2,499
  • 5
  • 34
  • 58
62
votes
5 answers

Put and get String array from shared preferences

I need to save on shared preferences some array of Strings and after that to get them. I tried this : prefsEditor.putString(PLAYLISTS, playlists.toString()); where playlists is a String[] and to get : playlist= myPrefs.getString(PLAYLISTS,…
Gabrielle
  • 4,933
  • 13
  • 62
  • 122
61
votes
5 answers

How to check if SharedPreferences exists or not

I'm checking in this way if the file exists, but I need to go beyond, I need to know if there is one in specific, is there any way? File f = new File("/data/data/com.eventrid.scanner/shared_prefs/Eventrid.xml"); if (f.exists()){ …
Nico1991
  • 637
  • 1
  • 5
  • 6
61
votes
4 answers

SharedPreferences application context vs activity context

I am using several SharedPreferences to store data in my app. Some preferences are used in a lot of activites. I know that the SharedPreferences are internally backed by a map for fast read-access and written to sdcard when settings are changed. I…
d1rk
  • 1,956
  • 2
  • 14
  • 19
59
votes
5 answers

Saving byte array using SharedPreferences

So I have a byte [] array which I have created in my android app. I want to use SharedPreferences from android to store it and retrieve it back again when I start my app. How can I do that ?
user2453055
  • 975
  • 1
  • 9
  • 19
56
votes
3 answers

Shared Preferences - max length of a single value

I am storing String into shared preferences, but I wasn't able to find out maximal length of string I can store. Do you know this value?
Waypoint
  • 17,283
  • 39
  • 116
  • 170
56
votes
4 answers

SharedPreferences and Thread Safety

Looking at the SharedPreferences docs it says: "Note: currently this class does not support use across multiple processes. This will be added later." So in and of itself it doesn't appear to be Thread Safe. However, what kind of guarantees…
cottonBallPaws
  • 21,220
  • 37
  • 123
  • 171
56
votes
3 answers

SharedPreferences not being removed on user uninstalling application

Has anyone encountered this issue on a Nexus 6P device? I am only getting this issue on a Nexus 6P (running Google Fi). When I install the app there is a key for userIsLoggedIn inside SharedPreferences. This block: boolean userIsLoggedIn =…
AndyRoid
  • 5,062
  • 8
  • 38
  • 73
54
votes
7 answers

Android - SharedPreferences with serializable object

I know that SharedPreferences has putString(), putFloat(), putLong(), putInt() and putBoolean(). But I need to store an object that is of type Serializable in SharedPreferences. How can I achieve this?
Carnal
  • 21,744
  • 6
  • 60
  • 75
53
votes
5 answers

Check if key exists in Shared Preferences

I'm creating Shared Preferences as follows preferences = getSharedPreferences("text", 0); final Editor editor = preferences.edit(); String s1 = serverIP.getText().toString(); String s2 = serverPort.getText().toString(); String s3 =…
Vivekanand
  • 755
  • 1
  • 8
  • 29
53
votes
6 answers

PreferenceActivity: save value as integer

Using a simple EditTextPreference in my preferences activity: Is there a way…
Laimoncijus
  • 8,615
  • 10
  • 58
  • 81
52
votes
6 answers

Can I get data from shared preferences inside a service?

I'm developing an android application. I'm using android 2.2 In my application I am capturing GPS data and sending it to service with the 1 hour time interval. If user exits from application it's also working (it is required). I'm using 2 services…
50
votes
10 answers

Best option to store username and password in android app

I am developing an Android app where the user needs to sign in to perform operations. But mostly on an android handset, people use "Keep me signed in", In that case, I'll have to maintain the value of Username and Password within my app. Should I…
50
votes
1 answer

Shared preferences inside broadcastreceiver

In my app,i want to use Shared Preferences inside a broadcast receiver...But i cant access the getPreferences() method inside... SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE); I cant call with the context object...any other…
subrussn90
  • 1,142
  • 1
  • 14
  • 27