Tuesday, December 6, 2016

Application level shared preference

A single class of shared preference which can store any type of data and objects at application level.

Example Code :

Shared Prefrence Class.

public class AppSharedPrefs
{

    private static AppSharedPrefs sharedPrefs = null;
    private SharedPreferences sf;


    public AppSharedPrefs(Context context)
    {
        sf = context.getSharedPreferences("key_shared_preference", Context.MODE_PRIVATE);
    }

    public static AppSharedPrefs getInstance(Context context)
    {
        if(sharedPrefs == null)
        {
            sharedPrefs = new AppSharedPrefs(context);
        }

        return sharedPrefs;
    }


    public Object get(String key)
    {
        Map<String, ?> map = sf.getAll();
        return map.get((String)key);
    }


    public void put(String key, Object value)
    {
        SharedPreferences.Editor edit = sf.edit();

        if(value == null)
        {
            edit.putString(key, null);
        }
        else if(value instanceof Boolean)
        {
            edit.putBoolean(key, (boolean)value);
        }
        else if(value instanceof Float)
        {
            edit.putFloat(key, (float)value);
        }
        else if(value instanceof Integer)
        {
            edit.putInt(key, (int)value);
        }
        else if(value instanceof Long)
        {
            edit.putLong(key, (long) value);
        }
        else if(value instanceof String)
        {
            edit.putString(key, (String)value);
        }
       else if(value instanceof  Model)
        {
            edit.putString(key, (Model)value);
        }
        else if(value instanceof Set)
        {
            edit.putStringSet(key, (Set)value);
        }

        edit.commit();
    }

    public void clearAll()
    {
        sf.edit().clear().commit();
    }

    public void clear(String key)
    {
        sf.edit().remove(key).commit();
    }
}


store various type of data in shared preference from any class as shown below.

public class RegisterActivity extends AppCompatActivity {

    private AppSharedPrefs sharedPrefs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        //initialise shared prefs manager
        sharedPrefs = AppSharedPrefs.getInstance(RegisterActivity.this);

        sharedPrefs.put("KeyName", "Zafar Imam");//stroing string value
        sharedPrefs.put("KeyAge", 24); //storing integer value
        sharedPrefs.put("IsRegister", true); //storing boolean value

        ModelClass model = new ModelClass();
        model.setFirstName("Zafar");
        model.setLastName("Imam");
        model.setRollNumber(512);

        //here you can store object of model class inside prefrence to transfer from one class to other
        sharedPrefs.put("Key_Serializable_Object", model);

    }
}

Model class of getter and setter: 

public class ModelClass implements Serializable {

    private String firstName;
    private String lastName;
    private int rollNumber;

    public int getRollNumber() {
        return rollNumber;
    }

    public void setRollNumber(int rollNumber) {
        this.rollNumber = rollNumber;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

Get the data from shared preference in other class as :


public class ProfileActivity extends AppCompatActivity {
    ModelClass modelClass;
    private AppSharedPrefs sharedPrefs;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile);

        //initialise shared prefs manager
        sharedPrefs = AppSharedPrefs.getInstance(ProfileActivity.this);

        Boolean isRegister = (Boolean) sharedPrefs.get("IsRegister");//getting stored data

        String name = (String) sharedPrefs.get("KeyName");//getting stored data

        int age = (Integer) sharedPrefs.get("KeyAge");//getting stored data

         modelClass = (ModelClass) sharedPrefs.get("Key_Serializable_Object");//getting object 

        if (isRegister){

            String firstName = modelClass.getFirstName();
            int rollNumber = modelClass.getRollNumber();
        }

    }
}

Note: for transferring Object from one class to other class, respected class(like ModelClass) must have implemented Serializable or Parcelable interface.

No comments:

Post a Comment