android sharedpreferences

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


I want to store a time value and need to retrieve and edit it. How can I use SharedPreferences to do this?



To obtain shared preferences, use the following method
In your activity:

SharedPreferences prefs = this.getSharedPreferences(
      "", Context.MODE_PRIVATE);

To read preferences:

String dateTimeKey = "";

// use a default value using new Date()
long l = prefs.getLong(dateTimeKey, new Date().getTime()); 

To edit and save preferences

Date dt = getSomeDate();
prefs.edit().putLong(dateTimeKey, dt.getTime()).apply();

The android sdk’s sample directory contains an example of retrieving and storing shared preferences. Its located in the:

<android-sdk-home>/samples/android-<platformversion>/ApiDemos directory


I noticed, it is important to write difference between commit() and apply() here as well.

commit() return true if value saved successfully otherwise false. It save values to SharedPreferences synchronously.

apply() was added in 2.3 and doesn’t return any value either on success or failure. It saves values to SharedPreferences immediately but starts an asynchronous commit.
More detail is here.


  • So the next time user runs my app, the stored value is there already and i can fetch it…right?

    Sep 2, 2010 at 7:09

  • 5

    (To anyone reading the above) Yes it is arbitrary. This example just saves the current date as a preference with the key “”.

    – MSpeed

    Jan 15, 2013 at 19:20

  • 1

    this.getSharedPreferences gives me the following error: The method getSharedPreferences(String, int) is undefined for the type MyActivity

    – Si8

    Jul 29, 2013 at 19:14

  • 17

    SharedPreferences.Editor.apply() was introduced in Gingerbread in November, 2010 (after this answer was posted). Use it instead of commit() where possible since apply() is more efficient.

    – UpLate

    Jul 31, 2013 at 21:04

  • 4

    Editor.apply() requires API level 9 or above. below that use Editor.commit()

    Dec 31, 2014 at 7:05


To store values in shared preferences:

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

To retrieve values from shared preferences:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String name = preferences.getString("Name", "");
    name = name + "  Sethi";  /* Edit the value here*/


  • 21

    I like this answer the best because it uses getDefaultSharedPreferences. For most users this will simplify things because the same preferences can be accessed throughout the entire app and you don’t have to worry about naming your preferences file. More on that here:

    Sep 1, 2014 at 15:03

  • 1

    I concur… I found this after pulling my hair out trying to figure out why I couldn’t access my shared prefs from another activity using the method in the accepted answer. Thanks so much!

    Dec 22, 2016 at 15:38

  • How can I use it to save and load Map<DateTime, Integer>?

    – Dmitry

    Aug 19, 2018 at 1:40

  • Use to simplify the implementation

    – Ali Asadi

    Dec 6, 2018 at 21:21


To edit data from sharedpreference

 SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
 editor.putString("text", mSaved.getText().toString());
 editor.putInt("selection-start", mSaved.getSelectionStart());
 editor.putInt("selection-end", mSaved.getSelectionEnd());

To retrieve data from sharedpreference

SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) 
  //mSaved.setText(restoredText, TextView.BufferType.EDITABLE);
  int selectionStart = prefs.getInt("selection-start", -1);
  int selectionEnd = prefs.getInt("selection-end", -1);
  /*if (selectionStart != -1 && selectionEnd != -1)
     mSaved.setSelection(selectionStart, selectionEnd);


I took this snippet from API Demo sample. It had an EditText box there . In this context it is not required.I am commenting the same .


  • 12

    +1, but use getPreferences(MODE_PRIVATE); instead of getPreferences(0); for readability.

    – Key

    Sep 2, 2010 at 6:35

  • What is mSaved here? I need to save 2 string values.

    Sep 2, 2010 at 6:36

  • I would also like to know what mSaved is. Nvm i think its the editbox

    Jun 5, 2012 at 0:19

  • 2

    what -1 means in getInt ??

    – amr osama

    Jul 17, 2014 at 9:51

  • 2

    Thats the default value going to be returned if the key(selection-start) doesn’t exist in sharedpreferences.It can be anything and is just for your reference.

    – DeRagan

    Jul 17, 2014 at 18:43