No description, website, or topics provided.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


SharedDataAccess library is for simple use of SharedPreferences in android

all thing you need is import library, and create interface with setter and getter methods

Also seeMy Android learning blog

Add it to your build.gradle with:

allprojects {
    repositories {
        maven { url "" }


dependencies {
    compile 'com.github.eiliyaabedini:lib-sharedDataAccess:1.0.2'

How to use

Create an interface with getter and setter methods like this

public interface SharedModel {
    String getToken();
    void setToken(String token);

Then you need to create an instance of SharedDataAccess in your Application class

SharedModel sharedModel = new SharedDataAccess(this).create(SharedModel.class);

Now you can use your method every where you want

set data to SharedPreferences


get data from SharedPreferences

String token = sharedModel.getToken();


  • Interface methods must start with set or get and then with a name which used for key.
  • Double not supported by SharedPreferences
  • Supported type is String, int, long, float, boolean
  • you can also set and get a custom Object with supported types fields.

Save an Object

  • To save an Object in SharedPreferences your class must have supported type fields.
  • Only public fields can access by SharedDataAccess and store data
  • Your class must have a default empty constructor

My custom Object:

public class MyObject {
    public int id;
    public String name;

    public MyObject(){


    public MyObject(int id,String name){ = id; = name;


public interface SharedModel {
    MyObject getMyObject();
    void setMyObject(MyObject myObject);

Create instance of SharedDataAccess

SharedModel sharedModel = new SharedDataAccess(this).create(SharedModel.class);

set data to SharedPreferences

sharedModel.setMyObject(new MyObject(1,"Test Data"));

get data from SharedPreferences

MyObject myObject = sharedModel.getMyObject();


Your interface can extends from SharedDefaultValues to have default value methods

public interface SharedModel extends SharedDefaultValues {
    String getToken();
    void setToken(String token);