Skip to content

Instantly share code, notes, and snippets.

@moizest89
Created February 13, 2019 14:22
Show Gist options
  • Save moizest89/9aeca581e21809e0c92df56b8e35daf7 to your computer and use it in GitHub Desktop.
Save moizest89/9aeca581e21809e0c92df56b8e35daf7 to your computer and use it in GitHub Desktop.
.
.
.
LocalSharedPreferences localSharedPreferences = new LocalSharedPreferences(this.context);
localSharedPreferences.setFirstName(owner.getFirstName());
localSharedPreferences.setLastName(owner.getLastName());
localSharedPreferences.setEmail(owner.getEmail());
.
.
.
public class LocalSharedPreferences implements SharedPreferences.OnSharedPreferenceChangeListener {
private final static String TAG = LocalSharedPreferences.class.getSimpleName();
private final String EMAIL = "email";
private final String FIRST_NAME = "first_name";
private final String LAST_NAME = "last_name";
public LocalSharedPreferences(Context context) {
this.context = context;
this.sharedPreferences = this.context.getSharedPreferences( this.context.getResources().getString(R.string.app_name) , Context.MODE_PRIVATE );
this.sharedPreferences.registerOnSharedPreferenceChangeListener(this);
}
public String getEmail() {
return this.sharedPreferences.getString(EMAIL, email);
}
public void setEmail(String email) {
this.sharedPreferences.edit().putString(EMAIL, email).commit();
}
public String getFirstName() {
return this.sharedPreferences.getString(FIRST_NAME, first_name);
}
public void setFirstName(String firstName) {
this.sharedPreferences.edit().putString(FIRST_NAME, firstName).commit();
}
public String getLastName() {
return this.sharedPreferences.getString(LAST_NAME, last_name);
}
public void setLastName(String lastName) {
this.sharedPreferences.edit().putString(LAST_NAME, lastName).commit();
}
// SharePreference interface implementation
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Log.e(TAG, "key: "+key );
}
public void showAllValues(){
Map<String, ?> allEntries = this.sharedPreferences.getAll();
Log.e(TAG, "allEntries: "+allEntries);
}
public static void deleteAllUserPreferences(Context context){
context.getSharedPreferences(context.getResources().getString(R.string.app_name),Context.MODE_PRIVATE)
.edit().clear().commit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment