20

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)?

Adesh Atole
  • 766
  • 1
  • 8
  • 22
  • 1
    Read the [docs](http://developer.android.com/reference/android/content/SharedPreferences.html). – Rahul Mar 29 '13 at 06:32
  • They're used as a way of [storage](http://developer.android.com/guide/topics/data/data-storage.html) – sinek Mar 29 '13 at 06:33
  • Related post - [Difference between preference and shared preference in Android](https://stackoverflow.com/q/23222931/465053) – RBT Aug 12 '18 at 07:46

5 Answers5

57

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.

Jay Mayu
  • 17,023
  • 32
  • 114
  • 148
15

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

Chirag
  • 56,621
  • 29
  • 151
  • 198
4

Main Purpose of SharedPreferences is to Store private primitive data in key-value pairs

you will get more idea from Below Link

Data-Storage in Android with Shared Preferences

Hope it Will Help you.

Bhavesh Patadiya
  • 25,740
  • 15
  • 81
  • 107
3

SharedPreferences is an API from Android SDK to store and retrieve application preferences. SharedPreferences are simply sets of data values that stored persistently. Persistently which mean data you stored in the SharedPreferences are still exist even if you stop the application or turn off the device. SharedPreferences available at the Activity level or shared across all Activity in application package.

Nirav Ranpara
  • 13,753
  • 3
  • 39
  • 54
2

First you should search it.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.

Link to developer site

Link to another stack overflow question and answer

Community
  • 1
  • 1
Android Killer
  • 18,174
  • 13
  • 67
  • 90