Previously you learned how to store data in databases. Databases are great if you want to query and manipulate the data. However, they add complexity to your project. What if you only wanted to persist the data as it is presently for later reading? Android offers a simpler solution for this type of storage, it is called SharedPreferences. In this video, you will learn what shared preferences is and how it can help you persist data. Mario and Adrian want to make the little lemon android app friendlier, to achieve this, they want to ask the user for their name if they haven't provided it yet. Once the user provides their name, the app should welcome them by using their name every time they return. Android provides a solution for storing simple types of data including Booleans, numbers, strings and string sets. SharedPreferences is a file storage interface that can be accessed from anywhere in your app where you have an android context reference. SharedPreferences is ideal for persisting simple data that you only want to store as is, such as the name of the user. The last time the app was started or user settings like a receive notifications preference. Thanks to shared preferences, support of strings and your knowledge of JSON, you can persist more complex data structures as well. All you have to do is convert the data to JSON before persisting it and convert it back when you want to read it. To use shared preferences in your app, you must first obtain an instance of it. You obtain an instance of shared preferences by calling getSharedPreferences on any android context such as the application and activity or a service. This function takes two arguments, the name of the file that stores the preferences and an operating mode flag. The first can be any valid file name and the second is commonly set to zero or context mode private, which means the file can only be accessed by applications sharing the same user ID as the current app. Other modes allow read or write access to all apps. You can also obtain the default shared preferences instance for your app by calling preferenceManager.getDefaultSharedPrefere- nces passing in any android context. Once you obtain an instance of shared preferences, you can start writing data to it. Data is written using an editor, the editor is responsible for saving any changes you made to the shared preferences file atomically. To get an instance of the editor, you call the edit function of the shared preferences instance you have. The editor exposes a few functions for storing data. Every data type has its own function including, putBoolean, putInt, and putString and others. Each function takes a unique key as a string and a value to persist. You can keep calling these functions until all the data you want to persist is added to the editor. You can remove values from shared preferences by calling remove, passing in the unique key of the value to remove. You can also remove all values by calling clear. To persist the changes in the editor, you call commit or apply, commit is synchronous and returns a Boolean indicating success or failure. Apply runs a synchronously and safely saves the changes in the background. You will not be notified if apply failed. If the sharedPreferences file did not exist prior to call and commit or apply, it will be created when these functions execute. SharedPreferences offers multiple functions for reading stored values. These include getBoolean, getString, getInt and others. Each function takes unique key used to save the value originally and a default value, which will be returned of no stored value was found. Shared preferences offers a get all function that returns a map of all stored keys and their values. It also offers a contains function which tells you whether a value is stored for a given key. Finally, sharedPreferences allows registering and unregistering listeners for changes. Let's look back to the little Lemon app upgrade. To implement the requested features, asking the user for their name and using their name to welcome them each time they return, you would first obtain a copy of shared preferences. This can be done from anywhere in your main activity by calling val sharedPreferences equal to preferenceManager.getDefaultShared preferences, this. You can then check is the user previously provided a name, by calling sharedPreferences.contains name. If a name exists, you can obtain it by calling sharedPreferences.getString name and present the user with a personalized greeting. If a name does not exist, you can ask the user for their name. Then, you can save their name using sharedPreferences.edit().putString name, name apply. In this video, you learned how sharedPreferences can be used to persist and restore data. Working with sharedPreferences will be a common activity in your career as you work with data in android development.