Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

What is shared preferences in android?

user-image
Question added by DHANISH PERUVANKUZHIYIL , Mobile App Developer , Mirnah Technology Systems
Date Posted: 2017/01/14

If you want to store setting/small amounts of data then shared preference is better to use.In shared preferences data stored in key-value pair.For an example you can store login credentials of user like username/password.In Shared preferences retrieving data is simple only key required but there is not a efficient way to search specific data .

SHIDHIN TS
by SHIDHIN TS , Android Developer , Wasltec

Shared preferences is a type of Database in Android. On this the values are saved ad Key Value pair. You can read more about Shared preferences from my Blog 

Share preferences is save all our work  on that application. 

Mohd Faisal
by Mohd Faisal , Senior Mobile Application Developer(Android/iOS/Flutter) , Naqel Express

In Android we use mostly uses 2 method to store data

1.database(sqlite,ormlite,android recommended sqlite) .

2.Shared preferences.

 

These two use depending on the situation 

If you want to store setting/small amounts of data then shared preference is better to use.In shared preferences data stored in key-value pair.For an example you can store login credentials of user like username/password.In Shared preferences retrieving data is simple only key required but there is not a efficient way to search specific data .

 

If you have a bulk data then it's better to use database system of android (Sqlite).we store data in table form in columns .In sqlite ,we can write queries to get specific record from tables. 

 

Sadia Qamar
by Sadia Qamar , Computer programer , Freelancer

It's  a small storage location 

Mohammad Faiz  Alam
by Mohammad Faiz Alam , Engineering Lead , Persistent Systems Limited

The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. 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).

User Preferences

Shared preferences are not strictly for saving "user preferences," such as what ringtone a user has chosen. If you're interested in creating user preferences for your application, see PreferenceActivity, which provides an Activity framework for you to create user preferences, which will be automatically persisted (using shared preferences).

To get a SharedPreferences object for your application, use one of two methods:

  • getSharedPreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter.
  • getPreferences() - Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don't supply a name.

To write values:

  1. Call edit() to get a SharedPreferences.Editor.
  2. Add values with methods such as putBoolean() and putString().
  3. Commit the new values with commit()
  4. To read values, use SharedPreferences methods such as getBoolean() and getString().
  5. Here is an example that saves a preference for silent keypress mode in a calculator:

    public class Calc extends Activity {    public static final String PREFS_NAME = "MyPrefsFile";    @Override    protected void onCreate(Bundle state){       super.onCreate(state);       . . .       // Restore preferences       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);       boolean silent = settings.getBoolean("silentMode", false);       setSilent(silent);    }    @Override    protected void onStop(){       super.onStop();      // We need an Editor object to make preference changes.      // All objects are from android.context.Context      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);      SharedPreferences.Editor editor = settings.edit();      editor.putBoolean("silentMode", mSilentMode);      // Commit the edits!      editor.commit();    }}

Deleted user
by Deleted user

*It is useful to store all kind of primitive data types.*Its lightweight.* Useful to store settings, one time registration password, flags in key value pair format. 

Saad Farooq
by Saad Farooq , Manager , Softarena Solutions

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

Kanaya Lal
by Kanaya Lal , Developer , BSQP TECH/E-Nexus solutions

Shared Preferences  Used to store data locally.

Rahul Patel
by Rahul Patel , Front End Developer , Mindtree Ltd

Main Purpose of  is to shared preferences in android is to Store private primitive data in key-value pairs

mahmoud Zahran
by mahmoud Zahran , Senior Android Developer , TEZ TOUR Egyp

Android provides many ways of storing data of an application. One of this way is called Shared Preferences. Shared Preferences allow you to save and retrieve data in the form of key,value pair.

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.

More Questions Like This