//This method will clear the sharedpreference
//It will be called on logout
public void logout() {
SharedPreferences.Editor editor = getSharedPreferences().edit();
editor.clear();
editor.apply();
}
Приведу пример своей реализации. Чать класс Utils:
class Utils {
public static SharedPreferences.Editor getSharedPrefEditor(Context context) {
return context.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE).edit();
}
public static String getCurrentUserRole(Context context) {
return getSharedPref(context).getString(User.ROLE, "");
}
public static String getCurrentUserPhone(Context context) {
return getSharedPref(context).getString(User.PHONE, null);
}
}
Код на добавление:
SharedPreferences.Editor sharedPrefEditor = Utils.getSharedPrefEditor(this);
sharedPrefEditor.putString(User.ROLE, user.getRole());
sharedPrefEditor.putString(User.USER_ID, firebaseUser.getUid());
sharedPrefEditor.putString(User.USER_NAME, user.getName());
sharedPrefEditor.putString(User.USER_EMAIL, user.getEmail());
sharedPrefEditor.putString(User.USER_AVATAR, user.getAvatarUrl());
sharedPrefEditor.putString(User.PHONE, user.getPhone());
sharedPrefEditor.apply();
Апостиль в Лос-Анджелесе без лишних нервов и бумажной волокиты
Основные этапы разработки сайта для стоматологической клиники
Продвижение своими сайтами как стратегия роста и независимости