87 Android Shared Preferences Tutorial | SharedPreferences in android app example | Part 2

preview_player
Показать описание
87 Android Shared Preferences Tutorial | SharedPreferences in android app example | Part 2
Nested classes of Shared Preferences:
-------------------------------------
SharedPreferences.Editor: Interface used to write(edit) data in SharedPreference file. Once editing has been done, one must commit() or apply() the changes made to the file.

SharedPreferences.OnSharedPreferenceChangeListener(): Called when a shared preference is changed, added, or removed. This may be called even if a preference is set to its existing value. This callback will be run on your main thread.

Methods of Shared Preferences:
------------------------------
contains(String key): This method is used to check whether the preferences contains a preference.

edit(): This method is used to create a new Editor for these preferences, through which you can make modifications to the data in the preferences and atomically commit those changes back to the SharedPreferences object.

getAll(): This method is used to retrieve all values from the preferences.

getBoolean(String key, boolean defValue): This method is used to retrieve a boolean value from the preferences.

getFloat(String key, float defValue): This method is used to retrieve a float value from the preferences.

getInt(String key, int defValue): This method is used to retrieve an int value from the preferences.

getLong(String key, long defValue): This method is used to retrieve a long value from the preferences.

getString(String key, String defValue): This method is used to retrieve a String value from the preferences.

getStringSet(String key, Set defValues): This method is used to retrieve a set of String values from the preferences.

registerOnSharedPreferencechangeListener(SharedPreferences.OnSharedPreferencechangeListener listener): This method is used to registers a callback to be invoked when a change happens to a preference.

unregisterOnSharedPreferencechangeListener(SharedPreferences.OnSharedPreferencechangeListener listener): This method is used to unregisters a previous callback.

Initialization
---------------
Editor to edit and save the changes in shared preferences.

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode

Storing Data:
-------------



Retrieving Data:
----------------
Data can be retrieved from saved preferences by calling getString() as follows:

Clearing or Deleting Data:
-------------------------
remove(“key_name”) is used to delete that particular value.

clear() is used to remove all data



OR

Рекомендации по теме
Комментарии
Автор

I have similar code but when I start the app I have to press retrieve each time. Is there any reason why my app does not load by default?

woonie
visit shbcf.ru