Best way to create SharedPreference
and for global usage you need to create a class like below:
public class PreferenceHelperDemo {
private final SharedPreferences mPrefs;
public PreferenceHelperDemo(Context context) {
mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
}
private String PREF_Key= "Key";
public String getKey() {
String str = mPrefs.getString(PREF_Key, "");
return str;
}
public void setKey(String pREF_Key) {
Editor mEditor = mPrefs.edit();
mEditor.putString(PREF_Key, pREF_Key);
mEditor.apply();
}
}