An easy to use wrapper tools for Shared Preference of android
from JCenter
or
Add dependency in your module build.gradle file
dependencies {
compile 'com.binjar.prefsdroid:prefs-droid:1.1.0'
}
Initialize preference in the Application
class
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
Preference.load().using(this).prepare();
}
}
If you wish to add custom name of the shared preference use
Preference.load().using(this).with(PREFERENCE_NAME).prepare();
After initialization, you can save values to the shared preferences from anywhere in your app, such as:
Preference.putString(TEXT, textInput.getText().toString());
or save an object
User user = new User(name, age);
Preference.putObject(OBJECT, user);
You can get the object by passing the class
of the User
User savedUser = Preference.getObject(OBJECT, User.class);
You can remove a single key
if (Preference.containsKey(KEY)) {
Preference.remove(KEY);
}
or remove all keys by calling
Preference.clear();
You can add @Type()
source annotation to your preference keys which will help you to find easily what type of value is stored against that key. See smaple PrefsKey.java for usage.
This project is licensed under the MIT License - see the LICENSE.md file for details