⇒ Android Shared Preferences is a powerful and efficient way to store data of your application inside the phone’s local storage.
- SharedPreference is the simplest mechanism to store the data in android.
- You do not worry about creating the file or using files API.
- It stores the data in XML files.
- SharedPreference stores the data in key value pair.
- The data is stored in XML file in the directory data/data//shared-prefs folder.
Application of SharedPreference
- Storing the information about number of visitors (counter).
- Storing the date and time (when your Application is updated).
- Storing the username and password.
- Storing the user settings.
The data that is stored will stay persistent inside your Android device unless you clear the app’s data or uninstall the app.
So, killing the app will not clear the data, please keep this in mind.
The implementation of Shared Preferences on Android is very simple and easy.
You only have to call the
getSharedPreferences() method from the Android framework. Then you can choose to read the data via get method or save data via edit method. Or you can even clear the data programmatically using remove method.
Save Data using Shared Preferences
private fun saveData() { val sharedPref = getSharedPreferences(PREF_NAME, MODE_PRIVATE) val editor = sharedPref.edit() val name = etName.text.toString() val age = etAge.text.toString().toInt() val isMarried = rbYes.isChecked editor.putString(NAME_KEY, name) editor.putInt(AGE_KEY, age) editor.putBoolean(MARRIED_KEY, isMarried) editor.apply() Toast.makeText(this, "Data is saved", Toast.LENGTH_SHORT).show() }
Read Data from Shared Preferences
class MainActivity : AppCompatActivity() { private val PREF_NAME = "MyPreferences" private val NAME_KEY = "name" private val AGE_KEY = "age" private val MARRIED_KEY = "married" override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get or retrieve data getData() } private fun getData() { val sharedPref = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE) // read or get the data from shared preferences val name = sharedPref.getString(NAME_KEY) val age = sharedPref.getInt(AGE_KEY, 0) val isMarried = sharedPref.getBoolean(MARRIED_KEY, false) // setting the data to EditText etName.setText(name) etAge.setText(age.toString()) // check the if already married or not if (isMarried) { rbYes.isChecked = true rbNo.isChecked = false } else { rbYes.isChecked = false rbNo.isChecked = true } } }
- When reading the data, you can use get methods. The get method will differ by its data type

- Clear Data from Shared Preferences
private fun clearData() { val sharedPref = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE) sharedPref.edit { remove(NAME_KEY) remove(AGE_KEY) remove(MARRIED_KEY) } Toast.makeText(this, "Data is cleared", Toast.LENGTH_SHORT).show() // refresh the data getData() }
Or You can Clear All SharedPreference Data with clear() function
sharedPref.edit().clear().apply()
Store Data Class object in SharedPreference
- note : Add library / Dependency for Gson
val gson = Gson() val json = gson.toJson(user) editor.putString("user", json)
and to get data class object from shared prefrence
val jobj = sp.getString("user",null) val user = Gson().fromJson(jObj)