Register now or log in to join your professional community.
what is shared preference in android
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 .
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.
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.
It's a small storage location
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).
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:
To write values:
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(); }}
*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.
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.
Shared Preferences Used to store data locally.
Main Purpose of is to shared preferences in android is to Store private primitive data in key-value pairs
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.