Get SharedPreferences data: // If value for key not exist then returns value null Storing data as KEY/VALUE pair: Editor editor = pref.edit() Įditor.putBoolean("key1", true) // Saving boolean - true/falseĮditor.putInt("key2", "int value") // Saving integerĮditor.putFloat("ke圓", "float value") // Saving floatĮditor.putLong("key4", "long value") // Saving longĮditor.putString("key5", "string value") // Saving string Or using PreferenceManager SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(context) MODE_WORLD_READABLEĬreate SharedPreferences: SharedPreferences prefs = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE)
How to use prefedit how to#
Now, lets take a look at how to use Android SharedPreferences class to store and retrieve application specific persistent data.Īvailable mode for shared preference 1. The data will be stored at data/data/package name/shared_prefs. If you uninstall the application, all your data will be lost, hence this is application specific. Preferences can be permanently stored using Android SharedPreferences in the form of a pair : key and a value, primitive data storage (boolean Strings, ints etc.). Each SharedPreferences file is managed by the framework and can be private or shared. In general, Android SharedPreferences is used to save small collection of key-values. In order to use shared preferences, you have to call getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences. Android SharedPreferences allows users to save and retrieve specific data of an application in the form of key,value pair.