Shared Preferences in Android App Development - IndianTechnoEra
Latest update Android YouTube

Shared Preferences in Android App Development

Shared Preferences in Android App Development, Indiantechnoera

What is Shared Preferences?

Shared Preferences in Android app development is a way to store and retrieve small amounts of data as key-value pairs.

It allows you to save simple data types such as strings, integers, and booleans to persistent storage that can be accessed throughout the app.

Here are some pros and cons of using Shared Preferences in Android app development:


Pros:

Easy to use: Shared Preferences are simple and easy to use. It is a lightweight storage mechanism and does not require complex setup or configuration.

Efficient: Shared Preferences provide a fast and efficient way to store and retrieve data.

Persistence: Shared Preferences store data persistently even when the app is closed, so data can be accessed across app sessions.

No external libraries: Shared Preferences is a part of the Android framework, so there is no need to include external libraries.


Cons:

Limited data storage: Shared Preferences are designed to store small amounts of data such as settings, preferences, and other simple values.

Not suitable for complex data: Shared Preferences are not suitable for storing complex data structures such as large lists or arrays.

Security concerns: Shared Preferences are not encrypted by default, so sensitive data should not be stored in them.


How to use?

Here's an example of how to use Shared Preferences in Android:

Create an instance of SharedPreferences:

SharedPreferences sharedPreferences = getSharedPreferences("my_preferences", MODE_PRIVATE);

Use SharedPreferences.Editor to add data:

SharedPreferences.Editor editor = sharedPreferences.edit();

editor.putString("key", "value");

editor.putInt("key2", 123);

editor.putBoolean("key3", true);

editor.apply();


Retrieve data from SharedPreferences:

String value = sharedPreferences.getString("key", "default_value");

int value2 = sharedPreferences.getInt("key2", 0);

boolean value3 = sharedPreferences.getBoolean("key3", false);


That's a brief overview of Shared Preferences in Android app development. I hope this helps!


Live Example:

MainActivity.java

Main Activity file to write logic and java code


activity_main.xml

In this .xml file we design the page and include the elements 


Output:

Post a Comment

Feel free to ask your query...
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.