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

Find Hours Diffrence in Kotlin

  In Kotlin, determining the difference in hours between two timestamps is a common task, especially in scenarios involving time-based calcu...