Shared preferences class.
Hello, everyone.
We're going to speak about the shared preferences 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, shared
preferences, classes, just one of them.
So in the shared preferences class, you can save primitive types of data, these are integer string,
boolean, all that kind of stuff.
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 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 shared preferences class and also we can reach
these save data types anywhere within the application.
So where can we use shared preferences class?
What do you suppose the better situation is to be using shared preferences class?
We can use the shared preferences class, especially in the sudden exits from an application.
Now, you must remember that we spoke about a life cycle 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'm going.
So the shared preferences 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 a message.
Somebody if you want to answer this phone call, your game application will pass to the on pause method
or Onestop 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 he can use the shared preferences class to save this kind of data, such as last score, higher score
game, a level, you know.
So let me give you another example of the shared preferences class, you can use this class in logging
operations, so most of the applications have a login page.
Yeah.
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 oh 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 shared preferences class.
So this feature provides user convenience, we'll call it, right.
Now, 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 switch
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 shared preferences
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 or she left off.
This is how you use shared preferences 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 shared preferences class.