1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. SharedPreferences & Data Saving in Android App Development

SharedPreferences & Data Saving in Android App Development


SharedPreferences & Data Saving

The course is part of this learning path

The SharedPreferences Class

This course explores how to both save and then retrieve data in your Android app. We'll look at storage in the SharedPreferences class as well as saving data to local memory. Finally, we'll see how to retrieve stored data.

Intended Audience

This course is intended for anyone who wants to learn how to start building their own apps on Android.


To get the most out of this course, you should have some basic knowledge of the fundamentals of Android.


Course GitHub repo: https://github.com/OakAcademy/android-app-development-with-kotlin/tree/main/Section%208%20-Shared%20preferences/SharedPreferences 


SharedPreferences Class. Hello everyone. We're going to speak about the SharedPreferences Class in this video. So, sit back. It's going to be a great ride. So, first let me tell you that Android provides many ways of storing data in an application. SharedPreferences Class is just one of them. So, in the SharedPreferences Class, you can save primitive types of data. These are integer, string, Boolean, all that kind of stuff. In the SharedPreferences Class, you can save and retrieve data in the form of key, value pair.

For example, if you want to store an integer type data, you will save this integer data type and a key that matches the data. You can save a relatively small collection of data in the SharedPreferences Class, and also we can reach these saved data types anywhere within the application. So, where can we use SharedPreferences Class? What do you suppose the better situation is to be using SharedPreferences Class? We can use the SharedPreferences Class especially in the sudden exits from an application. Now, you must remember that we spoke about a lifecycle in Android. So, a feature of Android applications is that you can switch between the applications. So, while you're working on one application, you can, let's say, take a phone call or make a message, or let's say, you want to just check something from another application. So, during these transitions, your applications can be closed, paused, or passed to the background.

You may need to save application data during these transitions, see where I am going? So, the SharedPreferences Class is very good for this type of situation. So, let's say, for example, while you're playing an Android game, you need to exit from the game suddenly, you can save your last score. You know how important that is. So, while you're playing a game, you can take a phone call or message somebody. If you want to answer this phone call, your game application will pass to the onPause method or onStop method.

When you come back to the game again, you should be able to continue where you left off, and also with your last score. So, you can use the SharedPreferences Class to save this kind of data, such as last score, highest score, game level. So, let me give you another example of the SharedPreferences Class. You can use this class in login operations. So, most of the applications have a login page. So, on this page, the user has to enter a username and password. If the user does not want to enter this information every time, the user can check a remember me checkbox, right? And then, after checking the checkbox, the application will save user data, and when the user comes back again to the application, the user will not have to write the username and password again. The application will remember it and write it automatically into the necessary field. Guess what? You save this kind of data using the SharedPreferences Class. So, this feature provides user convenience we'll call, right? Another example can be using mail or message applications. So, in these kinds of applications, user can write long texts. We've all got them. So, while the user is writing a long text, if the user needs to exit from the application or switches to another application, the text should not be deleted, right? So, before the user goes out of the application, you can save what the user wrote using the SharedPreferences Class. When the user comes back to the application, he'll be able to retrieve the data and then the user can continue where he/she left off. So, this is how you use SharedPreferences Class for these kinds of applications. So, I think the explanation is enough. So, let's get to Android Studio and see how to save local data using the SharedPreferences Class.


About the Author
Learning Paths

Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.

Covered Topics