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
50
votes
3 answers

Shared Preferences "limit"

I know similar question to this one has been asked a numerous times, and surfing through SO I partially found an answer, but not complete, and android docs don't really help. Obviously I know how they work and have used shared preferences many times…
Marko Niciforovic
  • 3,561
  • 2
  • 21
  • 28
49
votes
4 answers

Which Android Data Storage Technique to use?

The android documentation has the following options below but does not explain what circumstances each is best for. What are the pros and cons of each method? e.g. Under what conditions would SQL be better than Shared Preferences? Shared…
Declan McKenna
  • 4,321
  • 6
  • 54
  • 72
49
votes
12 answers

Android - How to use SharedPreferences in non-Activity class?

How do you use SharedPreferences in a non-Activity class? I tried making a generic Preferences utility class and importing android.content.Context but Eclipse still wouldn't let me use getSharedPreferences().
Jake Wilson
  • 88,616
  • 93
  • 252
  • 370
48
votes
3 answers

Android M weird shared preferences issue

On my Nexus 5 running Android M Developer Preview 2, when uninstalling/reinstalling an app, the device is retrieving shared preferences I stored long ago, for instance a boolean isFirstLaunch. The only way to clear these is to do it manually from…
nios
  • 1,153
  • 1
  • 9
  • 20
45
votes
11 answers

How to save List to SharedPreferences?
I have a list of products, which i retrieve from webservice, when app is opened for first time, app gets product list from webservice. I want to save this list to shared preferences. List medicineList = new ArrayList(); where…
kakajan
  • 2,614
  • 2
  • 22
  • 39
44
votes
3 answers

Android - How Do I Set A Preference In Code

I have an Android application in which I have my preferences in an XML file, which works fine. I now want to set one of the preferences using code instead of displaying the entire preference screen, how would I go about doing this?
stonedonkey
44
votes
7 answers

Android - Storing/retrieving strings with shared preferences

As the title says, I want to save and retrieve certain strings. But my code won't pass through the first line neither in retrieve or store. I tried to follow this link: http://developer.android.com/guide/topics/data/data-storage.html private void…
Rad
  • 830
  • 1
  • 12
  • 24
43
votes
4 answers

Get Android shared preferences value in activity/normal class

I have made a shared preference activity that store the user settings, now i want to get values in a activity or normal java class.please provide a solution or example i have already tried this code but failed. public void onCreate(Bundle…
Sandeep
  • 2,573
  • 3
  • 21
  • 28
43
votes
3 answers

Android: Is it a good idea to store Authentication Token in Shared Preferences?

I have an application that communicates with a server. When the user logins to the application an Authentication Token is crated on the server and stored in the SharedPreferences of the application and whenever the application requests data from a…
Eric Bergman
  • 1,453
  • 11
  • 46
  • 84
42
votes
2 answers

How to Iterate through all Bundle objects

I am trying to create helper method that would iterate through all Bundle objects, in a generic manner. By "generic" I mean: Doesn't need to know the names (keys) of the objects in the Bundle passed as a parameter. Doesn't need to change if another…
ateiob
  • 9,016
  • 10
  • 44
  • 55
41
votes
11 answers

Android - How do I get sharedpreferences from another activity?

In my app there is a button (activity1). When user clicks it, I want no sound in the game. I thought I should do this by using sharedpreferences in activity1 in the onClick method of the button: SharedPreferences.Editor editor =…
erdomester
  • 11,789
  • 32
  • 132
  • 234
41
votes
4 answers

How to use Shared Preferences in MVP without Dagger and not causing Presenter to be Context dependent?

I'm trying to implement MVP without Dagger (for learning purposes). But I got to the problem - I use Repository patter to get raw data either from cache (Shared Preferences) or network: Shared Prefs| …
41
votes
3 answers

How to mock a SharedPreferences using Mockito

I have just read about Unit Instrumented Testing in Android and I wonder how I can mock a SharedPreferences without any SharedPreferencesHelper class on it like here My code is: public class Auth { private static SharedPreferences loggedUserData =…
adamura88
  • 513
  • 1
  • 4
  • 13
40
votes
9 answers

Is it ok to save a JSON array in SharedPreferences?

I have a JSON Array that I need to save. I was thinking about serializing it, but would it be better to just save it as a string in SharedPreferences and then rebuild it when I need to read it in?
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
39
votes
5 answers

Read speed of SharedPreferences

How fast are SharedPreferences? Is there a way to put them in memory for reading? I have a small amount of data that a ListView has to query to display each cell, and I'm worried that a call to flash memory will be too slow. I'm not worried about…
GLee
  • 5,003
  • 5
  • 35
  • 39