Essential Android SharedPreferences edit forum

Store Retrieve Remove and Clear Data from SharedPreferences

Create SharedPreferences BuyyaPref

SharedPreferences pref = getApplicationContext().getSharedPreferences("BuyyaPref", MODE_PRIVATE); 
Editor editor = pref.edit();

Storing data as KEY/VALUE pair

editor.putBoolean("key_name1", true);           // Saving boolean - true/false
editor.putInt("key_name2", 10);        // Saving integer
editor.putFloat("key_name3", 10.1f);    // Saving float
editor.putLong("key_name4", 1000);      // Saving long
editor.putString("key_name5", "MyString");  // Saving string
 
// Save the changes in SharedPreferences
editor.commit(); // commit changes

Get SharedPreferences data

If value for key not exist then return second param value(In this case null, this is like default value)

pref.getBoolean("key_name1", null);         // getting boolean
pref.getInt("key_name2", null);             // getting Integer
pref.getFloat("key_name3", null);           // getting Float
pref.getLong("key_name4", null);            // getting Long
pref.getString("key_name5", null);          // getting String

Deleting Key value from SharedPreferences

editor.remove("key_name3"); // will delete key key_name3
editor.remove("key_name4"); // will delete key key_name4
 
// Save the changes in SharedPreferences
editor.commit(); // commit changes

Clear all data from SharedPreferences

editor.clear();
editor.commit(); // commit changes

Table Of Contents
14 SharedPreferences
39 ACRA
64 Menu
112 Loader
119 Xposed
132 Colors
135 Fresco
140 AdMob
147 Button
156 Vk SDK
170 XMPP
176 OpenCV
200 FileIO
203 Moshi
217 Paint
231 AIDL
241 JCodec
243 Okio
255 Looper
  ↑ ↓ to navigate     ↵ to select     Esc to close