in

Shared Preferences Class in Android

Hello everyone. We will talk about the shared preferences class in this article.

Android provides many ways of storing data of an application. Shared Preferences Class is one of them. In the Shared Preferences Class, you can save the primitive type of data. These are an integer, string, boolean and etc.

In the Shared Preferences Class, you can save and retrieve data in the form of key-value pair. For example, If you want to store an integer data, you will save this integer data and a key that matches with the data.

You can save a relatively small collection of data in the Shared Preferences Class. And also we can reach these saved data anywhere in the application.

So, where can we use Shared Preferences Class? Which situation is better to use Shared Preferences Class.

We can use Shared Preferences Class, especially in the sudden exits from the application. A feature of Android Applications is that you can switch between the applications. While you are working on an application, you can take a phone call or a message or you want to check another application. During these transitions, your applications can be closed, paused or pass to the background. You may need to save application data during these transitions. Shared Preferences Class is very good for such cases.

For example, while you are playing an Android game. If you need to exit from the game suddenly, you can save your last score. While you are playing a game, you can have a phone call or message. 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 continue where you left and also with your last score. You can Shared Preferences Class to save this kind of data such as last score, highest score, game level and etc.

We can give another example of the Shared Preferences Class. You can use this class in the login operations. Most of the applications have a login page. On this page, the user has to enter a username and a password. If the user does not want to enter this information every time, the user can check a remember me checkbox. After checking this checkbox, the application will save user data and when the user comes back again to the application, the user will not have to write username and password again. The application will remember it and write it automatically in the necessary fields.

You can save this kind of data using Shared Preferences Class. This feature provides the user convenience.

Another example can be about mail or message applications. In this kind of application, the user can write long texts. 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. Before the user goes out of the application, you can save what the user wrote using Shared Preferences Class. When the user comes back to the application, you can retrieve the data and the user can continue where left.

You can use Shared Preferences Class in this kind of application too.

So, Shared Preferences Class is important for Android Developers. You should learn and use this feature. See you in the next article.

Written by Kadir Yapar

I am an Android Developer. I graduated from Sakarya University Physics Department in 2006. I completed my master degree in computer engineering between 2009 and 2011. I started my PhD education in the field of COMPUTER ENGINEERING IN SELÇUK UNIVERSITY INSTITUTE OF SCIENCE IN 2018. Between the years of 2015-2016, I worked as a research assistant at the Department of Computer Engineering at Bursa Orhangazi University. I have been working in mobile application development for the last 3 years. I published mobile application development courses on the Udemy platform. Also I published many android applications on Google Play.