Last Updated:

SharedPreferences in Android: save settings

In this tutorial we will use to store user data as key-value pairs.SharedPreferences

SharedPreferences on Android. Theory

SharedPreferences Allow applications to save settings as key-value pairs. These settings are preserved even when the user closes the application.

Android stores as an XML file in the folder shared_prefs at . You can get the data folder by calling the .SharedPreferences data --> data --> [имя пакета] -->Environment.getDataDirectory()

 

SharedPreferences depend on the application, that is, the saved data will be deleted from the Android device after uninstalling the application or after wiping the application data.

SharedPreferences API Overview

Android provides three methods for accessing SharedPreferences:

  • The method is used inside an Activity to access a single shared application settings file.getPreferences()
  • The method is used inside the Activity to access different application configuration files.getSharedPreferences()
  • The method is used by the class to access the standard settings file.getDefaultSharedPreferences()PreferenceManager

In this tutorial we will learn more about the method.getSharedPreferences()

This method is used as follows:

where is the name of the file, and is how you want to gain access.prefs_namemode

In total, Android provides several ways to gain access:

  • MODE_PRIVATE is the default mode where the generated file can only be accessed by the calling application
  • MODE_WORLD_READABLE – creating files that can be accessed for reading from any other application (a dangerous method that often leads to problems in the security of the application)
  • MODE_WORLD_WRITEABLE is the creation of files that can be accessed (read and write) from any other application.
  • MODE_MULTI_PROCESS was marked as deprecated in Android API 23 because it did not work correctly in some versions of Android and was not thread-safe.
  • MODE_APPEND adds new settings to existing settings.

SharedPreferences on Android. Practice

 

Starting with this tutorial, we'll move the text or names of the widgets used in the layout into a file that is located in your project's folder. This is a good programming practice for Android that we will follow. Here is the file for our project:strings.xmlres/valuesstrings.xml

The layout consists of one widget and three buttons for saving, clearing and retrieving user data from: activity_main.xmlEditTextSharedPreferences

Notice that we have assigned the same method in the attribute. Below in the class listing, you'll see how we handle clicks for each button:onClickMyActivity.class

Note that we implement the interface and override the method in which we use the switch statement to find the id of the widget that was clicked. In the layout, this is the method we have assigned common to all buttons.View.OnClickListeneronClick()activity_main.xml

The result of the application saving SharedPreferences settings in Android:

SharedPreferences

As you can see, the figure shows the progress of operations: first, write the text in the field (1), then click Save (2), then click Clear (3) and at the end we test getting the settings from SharedPreferences - click Get (4).

That's all, follow the updates of the programming section for Android and subscribe to the newsletter of the site!