Skip to content

Preference Tutorials

Mohit Rajput edited this page Jan 27, 2019 · 1 revision

Preference Tutorials

This library is used to make storing and retrieving values from the shared preference easy.

Get Preference Instance

The following method returns the instance of preference utils-

SharedPreferenceUtils sharedPreferenceUtils = SharedPreferenceUtils.getInstance(context);

SharedPreferenceUtils returns the singleton instance which is used to store and retrieve.

Save Values

SharedPreferenceUtils.getInstance(context).saveBoolean("is_logged_in", true);

Retrieve Values

boolean isLoggedIn = SharedPreferenceUtils.getInstance(context).getBoolean("is_logged_in");

To retrieve with default values-

boolean isLoggedIn = SharedPreferenceUtils.getInstance(context).getBoolean("is_logged_in", false);

To clear a value- SharedPreferenceUtils.getInstance(context).clear("is_logged_in");

To clear all values from preference-

SharedPreferenceUtils.getInstance(context).clear();

Preference utility has methods for all primitive data types and String.

Clone this wiki locally