Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of Shared Preferences in Android? [closed]

Tags:

android

I am just wondering, what is the use of shared preferences in Android. Please some one clear my doubt.

EDIT

Can I use them for the purpose like Showing the setup screen for the first time, and setting some flags, so that the setup screen/activity won't be shown after it is completed(on next startup of app)?

like image 258
Adesh Atole Avatar asked Mar 29 '13 06:03

Adesh Atole


People also ask

What is shared preferences used for in Android?

A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. Each SharedPreferences file is managed by the framework and can be private or shared. This page shows you how to use the SharedPreferences APIs to store and retrieve simple values.

When should I use shared preferences?

SharedPreferences are used in android to store some data presistently(i.e. after closing of application, it will persist). If you want to store few amount of data then you can go for SharedPreferences rather than going for Sqlite and all.In that case SharedPreferences are useful.

When the shared preference file got deleted from device?

when you do clear data from the device applications manager or when you uninstall your application, the SharedPreference's file is deleted. unless you have the android:allowBackup="true" in your manifest. In that case they might be restored.

Does uninstalling app clear shared preferences?

The shared preference is definitely deleted when the application is uninstalled.


2 Answers

Think of a situation where you wanna save a small value (a flag probably) that you wanna refer later sometime when user launches the application. Then shared preference comes into action.

You may ask, why we can do it using sqlite too right? But the problem of sqlite is that it’ll want you to write lengthy codes and supporting classes. Shared Preference let you read and write key value pair in couple of lines easily. But always remember, shared preference is not a solution for you to keep complex relational data.

like image 74
Jay Mayu Avatar answered Sep 29 '22 10:09

Jay Mayu


You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).

Read More

like image 28
Chirag Avatar answered Sep 29 '22 09:09

Chirag