Thursday, 8 August 2013

Use SharedPrefrence from Common Class

public static void setSharedKey(String key, String value, Context context) {
        SharedPreferences spre = context.getSharedPreferences(
                context.getPackageName(), Context.MODE_PRIVATE);
        SharedPreferences.Editor prefEditor = spre.edit();
        prefEditor.putString(key, value);
        //prefEditor.clear();
        prefEditor.commit();
    }

    public static String getSharedKey(String key, Context context) {
        SharedPreferences spre = context.getSharedPreferences(
                context.getPackageName(), Context.MODE_PRIVATE);
        return spre.getString(key, "");
    }



Declare this above two method in your common class 

to set shared preference

Utility.setSharedKey("key", "value", mContext);

to get shared preference

String mypref = Utility.getSharedKey("key", MainActivity.this);

 

No comments:

Post a Comment

Comments

Snackbar in Kotlin

How to show snackbar in place of toast message , below is the code for showing snackbar in kotlin. Below code is put in Utils.kt  (common fi...