In Android , Shared preference can be used to store values in a key-value pair. We can store values in a private mode such that, it is not accessible to any other apps.
- Create the shared preference in private mode.
- Initialize the editor mode.
- Set values to your key.
- Finally commit your values.
- Get the saved values using the key.
- Set default value to return if key value doesn't exist.
To set values to Shared Preference:
SharedPreferences pref = getSharedPreferences("sample", 0);
SharedPreferences.Editor editor = mVoice.edit();
editor.putString("key1", "value1");
editor.putString("key2", "value2");
editor.commit();
To get values from Shared Preference:SharedPreferences pref = getSharedPreferences("sample", 0);
String start = pref .getString("key1", "Not set");String stop = pref .getString("key2", "Not set");