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
598
votes
30 answers

How to use SharedPreferences in Android to store, fetch and edit values

I want to store a time value and need to retrieve and edit it. How can I use SharedPreferences to do this?
Muhammad Maqsoodur Rehman
  • 33,681
  • 34
  • 84
  • 124
586
votes
29 answers

How to delete shared preferences data from App in Android

How do I delete SharedPreferences data for my application? I'm creating an application that uses a lot of web services to sync data. For testing purposes, I need to wipe out some SharedPreferences values when I restart the app.
Andrew
  • 20,756
  • 32
  • 99
  • 177
508
votes
9 answers

What's the difference between commit() and apply() in SharedPreferences

I am using SharedPreferences in my android app. I am using both commit() and apply() method from shared preference. When I use AVD 2.3 it shows no error, but when I run the code in AVD 2.1, apply() method shows error. So what's the difference…
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
386
votes
6 answers

How do I get the SharedPreferences from a PreferenceActivity in Android?

I am using a PreferenceActivity to show some settings for my application. I am inflating the settings via a xml file so that my onCreate (and complete class methods) looks like this: public class FooActivity extends PreferenceActivity { …
Dave
  • 3,983
  • 3
  • 17
  • 8
374
votes
38 answers

Save ArrayList to SharedPreferences

I have an ArrayList with custom objects. Each custom object contains a variety of strings and numbers. I need the array to stick around even if the user leaves the activity and then wants to come back at a later time, however I don't need the array…
ryandlf
  • 27,155
  • 37
  • 106
  • 162
334
votes
14 answers

Shared preferences for creating one time activity

I have three activities A, B and C where A and B are forms and after filling and saving the form data in database (SQLite). I am using intent from A to B and then B to C. What I want is that every time I open my app I want C as my home screen and…
user3104719
  • 3,465
  • 3
  • 13
  • 9
295
votes
24 answers

How do you save/store objects in SharedPreferences on Android?

I need to get user objects in many places, which contain many fields. After login, I want to save/store these user objects. How can we implement this kind of scenario? I can't store it like this: SharedPreferences.Editor prefsEditor =…
Piraba
  • 6,974
  • 17
  • 85
  • 135
233
votes
6 answers

Difference between getDefaultSharedPreferences and getSharedPreferences

What is the difference between getDefaultSharedPreferences and getSharedPreferences in Android? Can anyone please explain?
James
  • 2,383
  • 2
  • 14
  • 6
218
votes
9 answers

PreferenceManager getDefaultSharedPreferences deprecated in Android Q

PreferenceManager getDefaultSharedPreferences is deprecated in Android 10. How do I replace it?
Martynas B
  • 2,843
  • 2
  • 12
  • 15
183
votes
5 answers

Pros and Cons of SQLite and Shared Preferences

What is the good mechanism to store information among SQLite database and Shared Preferences? Why use shared preferences? Why use sqlite? I tried to find the difference between them, and which is the better mechanism for data storing, but I am…
Rana.S
  • 2,265
  • 3
  • 20
  • 19
172
votes
8 answers

Where are shared preferences stored?

Where in an Eclipse project might one encounter a shared preferences file?
farm ostrich
  • 5,881
  • 14
  • 55
  • 81
166
votes
15 answers

How can I view the shared preferences file using Android Studio?

I'm using shared preferences to store certain values for my app. I would like to see the file where the info is actually stored on my phone. I found many ways to do this on Eclipse, but I'm debugging on Android Studio. My phone is rooted. I read…
JayUser
  • 1,661
  • 2
  • 11
  • 3
138
votes
4 answers

Android SharedPreference security

I wonder about shared preferences security. Is it possible to get access to sharedpreferences, even if they were created in MODE_PRIV (0) ? Is it possible to list all sharedpreferences available and then fetch all settings from other apps? Is…
Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
131
votes
6 answers

Should accessing SharedPreferences be done off the UI Thread?

With the release of Gingerbread, I have been experimenting with some of the new API's, one of them being StrictMode. I noticed that one of the warnings is for getSharedPreferences(). This is the warning: StrictMode policy violation; ~duration=1949…
cottonBallPaws
  • 21,220
  • 37
  • 123
  • 171
131
votes
4 answers

iOS Equivalent For Android Shared Preferences

I am porting an Android app to iOS, one thing I used was the Shared Preferences in Android to save each time a level was complete. That way when the user gets back into the app, they can see they are up to level 3 or whatever. Is there a similar…
ddoor
  • 5,819
  • 9
  • 34
  • 41
1
2 3
99 100