Bespoke Android App Developers: SharedPreferences API

Bespoke Android App Developers: SharedPreferences API

Android SharedPreferences is an API used to store and retrieve small key-value pairs of data within an Android application. SharedPreferences is a part of the Android framework and is used to store preferences or settings for an application. The SharedPreferences API stores the data as a file in the application's private directory, which means that the data is only accessible by the application itself.

To use the SharedPreferences API, you need to follow these steps:

  1. Obtain an instance of the SharedPreferences class by calling the getSharedPreferences() method on the Context object. The first parameter of this method is a string that represents the name of the preferences file. The second parameter is an integer that represents the mode in which the file should be created, which can be either MODE_PRIVATE or MODE_MULTI_PROCESS.

    SharedPreferences sharedPrefs = getSharedPreferences("myPrefsFile", MODE_PRIVATE);
  2. To store a value, you can use one of the put methods of the SharedPreferences.Editor class. The put method takes two parameters: the key and the value.

    SharedPreferences.Editor editor = sharedPrefs.edit(); editor.putString("username", "John"); editor.putInt("age", 30); editor.apply();
  3. To retrieve a value, you can use one of the get methods of the SharedPreferences class. The get method takes one parameter: the key.

    String username = sharedPrefs.getString("username", ""); int age = sharedPrefs.getInt("age", 0);
  4. To remove a value, you can use the remove method of the SharedPreferences.Editor class.

    SharedPreferences.Editor editor = sharedPrefs.edit(); editor.remove("username"); editor.apply();
  5. To clear all the values in the preferences file, you can use the clear method of the SharedPreferences.Editor class.

    SharedPreferences.Editor editor = sharedPrefs.edit(); editor.clear(); editor.apply();

It is important to note that SharedPreferences should only be used to store small amounts of data, such as settings or preferences. If you need to store large amounts of data, you should consider using a SQLite database or another form of persistent storage.


The SharedPreferences API is a way for Android developers to store key-value pairs of primitive data types such as boolean, int, float, long, and String in an XML file called SharedPreferences. This allows developers to store small amounts of data that can be accessed and modified throughout the application.

To use SharedPreferences, you can follow these steps:

  1. Get an instance of SharedPreferences:

    SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);

    The first parameter is the name of the preferences file, and the second parameter is the access mode. The access mode determines the visibility of the preferences file to other applications.

  2. Edit the preferences:

    SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("key", "value"); editor.putInt("key2", 1); editor.putBoolean("key3", true); editor.apply();

    You can add or modify key-value pairs using the SharedPreferences.Editor class. Call the apply() method to save the changes.

  3. Retrieve the preferences:

    String value = sharedPreferences.getString("key", ""); int value2 = sharedPreferences.getInt("key2", 0); boolean value3 = sharedPreferences.getBoolean("key3", false);

    Use the get methods of the SharedPreferences class to retrieve the values of the keys. The second parameter of the get methods is the default value if the key is not found.

That's it! You can use the SharedPreferences API to store and retrieve small amounts of data that need to be accessed throughout the application.

Read more about SharedPreferences API