Getting started with Shared Preferences in Android

Hello friends, today we are going to learn about Shared Preferences in Android. Now let us first be clear about

What are Shared Preferences?

So basically, Shared Preferences are key-value pairs of primitive data types that are saved in a file within an apps file structure. You can then access this file from anywhere within the app to either edit data or delete data. You can’t access the file from another app so it’s pretty secure from that point of view.

Where to use Shared Preferences?

For an example use SharedPreferences in a login page where you would save the user’s name and the user’s password even after getting logged out like we normally see in many apps, it’s like one time login. Then the next time they log in, they do not need to enter the credentials it will automatically directed to the dashboard of the app, so basically the credentials would be retrieved from the shared preference file and you would continue after that and you don’t have to login hundreds of time.

Hope you understood the theory behind it , now let’s get started with the coding part.

In order to use shared preferences,
you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.

Now storing the data in shared preferences in key value pair

Getting shared preferences data

Deleting key/value pairs from shared preferences

That’s all for the post. Enjoy learning 🙂

 

 

Hey there, I’m Sobhini, a Graduate Engineer. I post here very often, fields of interest lies in android programming, web development and UI/UX designing. Follow me to get the latest updates 😉

Sobhini Reddy

Hey there, I'm Sobhini, a Graduate Engineer. I post here very often, fields of interest lies in android programming, web development and UI/UX designing. Follow me to get the latest updates ;)

Leave a Reply

Your email address will not be published. Required fields are marked *