Tuesday, October 15, 2024
HomeAndroidAndroid SharedPreferences Example - Android Storage Types Tutorial #1

Android SharedPreferences Example – Android Storage Types Tutorial #1

-

Android SharedPreferences provides an easy and efficient way to store data in the form of Key value pairs in Android. Unlike other storage options whose setup is a bit complex, SharedPreferences provides an easy way to read and write data into a file.

What are SharedPreferences?

SharedPreferences are just key value pairs which are stored in an xml file. If we know the key, we can access the value.

android sharedpreferences

What kind of data can I store using Android SharedPreferences?

We can store all primitive data types like int, float, long, boolean, String in SharedPreferences. For data that is complex and bulkier than this, it is better to use sqlite, which we’ll also discuss in my future tutorials.

Accessing Android SharedPreferences

Accessing Android SharedPreferences can be done by using the following code:

//Accessing android sharedpreferences
Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences("MY_KEY", Context.MODE_PRIVATE);

We can observe that from the above lines of code, getSharedPreferences() method from context takes in two parameters:

  • Key
  • Mode

Key – It is the one which is used to save the datatype in SharedPreferences

Mode – Which sets whether the data should be visible to other apps. MODE_PRIVATE is the default mode.

Different Types of Modes Available:

1. MODE_APPEND:

File creation mode: for use with openFileOutput(String, int), if the file already exists then write data to the end of the existing file instead of erasing it.

2. MODE_ENABLE_WRITE_AHEAD_LOGGING:

Database open flag: when set, the database is opened with write-ahead logging enabled by default.

3. MODE_PRIVATE:

File creation mode: the default mode, where the created file can only be accessed by the calling application (or all applications sharing the same user ID).

4. MODE_NO_LOCALIZED_COLLATORS:

Database open flag: when set, the database is opened without support for localized collators.

Writing data to Android SharedPreferences

To write data to Android sharedPreferences we need to create SharedPreferences.Editor by calling edit() method from SharedPreferences as follows:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("MY_KEY", "Vamsi Tallapudi");
editor.commit();

Reading data from Android SharedPreferences

//reading data from android sharedPreferences
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
String myName = sharedPref.getString("MY_KEY", "my default value");

 

So where is my data stored?

The data for the respective app will be stored in the following directory:

/data/data/<package_name>/shared_pref/

Example Please:

Here is the video so that it would be easier to understand Android SharedPreferences with an example.

 

The sourcecode for the example created in the above Youtube Video on Android SharedPreferences is in the below link pointing to github.

 

 

This completes our tutorial on SharedPreferences. We’ll discuss about various other storage options available in Android in upcoming articles.

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

LATEST POSTS

Single Number | Leetcode 136 | 100 Days of Code Day 9

Welcome to Day 9 of Coderefer's 100 Days of code where we will be solving problems from Coderefer DSA sheet each day and today we...

3 Sum | Leetcode #15 | 100 Days of Code Day 10

Today we are going to solve 3 Sum - our first Medium Level Array problem from Coderefer DSA Sheet. If you are new to this...

Best Time to Buy and Sell Stock – Day 7 | 100 Days of Code

Welcome to Day 7 of 100 Days of Code where today we solve yet another most frequently asked easy array problem called - Best Time...

Contains Duplicate – Day 8 | 100 Days of Code

Welcome to Day 8 of 100 Days of Code and today we are going to solve yet another easy-level problem - Contains Duplicate as part...

Follow us

1,358FansLike
10FollowersFollow
397SubscribersSubscribe

Most Popular