0

I'm trying to save information on an Android app I've made. I want to save a name, "Robert". For this I've been looking into Shared Preferences and I can't find a tutorial that explains how to create SharedPreferences.

All tutorials start like this:

SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);

But they don't explain where the getPreferences() take the object from.

When is this object (SharedPreferences object) created? Is it created along with the context? Is it created together with each activity?

I'm pretty new to Android, but an intermediate(minus) Java programmer.

Einar
  • 1,001
  • 3
  • 11
  • 28

2 Answers2

2

SharedPreferences are created like this:

SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
 editor.putString("name", "Robert");
 editor.commit(); //Or use editor.apply()

Then you get them again like this:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
  String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.

So, the getString() gets the value you stored before, and returns the default value if you haven't stored a string for 'name' yet.

The object you store is saved in the system, and is constantly available to be grabbed.

UPDATE: The getSharedPreferences() method returns a SharedPreferences.Editor interface. According to the android docs

Interface used for modifying values in a SharedPreferences object. All changes you make in an editor are batched, and not copied back to the original SharedPreferences until you call commit() or apply()

UPDATE 2: This answer contains more info on the storage of SharedPreferences.

Community
  • 1
  • 1
Sub 6 Resources
  • 1,674
  • 15
  • 31
  • Your first code paragraph shows how to create an editor. I'm more curious about the SharedPreferences object that the getSharedPreferences() comes up with. You mention that it returns a default value if nothing is stored beforehand, is this a default object in the activity? – Einar Nov 17 '16 at 22:40
  • The getSharedPreferences() method returns the SharedPreferences.Editor interface. I'll update my answer with more info about the interface. – Sub 6 Resources Nov 17 '16 at 22:45
  • @Einar If you leave off the preferences name, then you have preference local to the Activity. It creates or opens an XML file. – OneCricketeer Nov 17 '16 at 22:46
  • So far I understand that a SharedPreference object stores keysets - like ("name", "Robert"). I also understand that if you commit and editor it will store that SharedPreference object. If you want to fetch the previously commited object, then you call getSharedPref(). But if there is no object there to begin with, what do you call with getSharedPref() the first time around? Why is there no SharedPreferences prefs = new SharedPreference()? Did the activity create this for me? – Einar Nov 17 '16 at 22:57
  • When you download an app to your Android device, it automatically creates some file in the file system. The SharedPreferences file is one of these. It is empty, but is there waiting to be accessed. – Sub 6 Resources Nov 17 '16 at 22:58
  • But the 'SharedPreferences prefs' doesn't point directly to the file, right? When is the object that points to the file, created? – Einar Nov 17 '16 at 23:02
  • I'm not exactly sure what happens behind the scenes in the SharedPreferences class. My guess is that the class is a static class, and does not need to be instantiated. – Sub 6 Resources Nov 17 '16 at 23:06
1

SharedPreferences are stored in your app's data folder as an xml file. It doesn't matter what context you use to getSharedPreferences from. It will pull those preferences from that file. Once loaded for the first time, the preferences file is cached process-wide so you will get the same object back on each subsequent getSharedPreferences call (even if they are from different Activities).

More information here and here.

Community
  • 1
  • 1
James McCracken
  • 15,488
  • 5
  • 54
  • 62