Issue
I'm storing each user shared preferences based on what they do with the app, the users can have multiple Gmail accounts and I'm trying to manage sharedpreferences for each account.
I managed to do a copy of my defaultsharedpreferences into each of my users email preference, the thing is that the users preferences never change when I do something with the app, but the default preferences does.
Here is my code:
sharedPrefsDefault = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
sharedPrefs = getSharedPreferences(sharedPrefsDefault.getString(getString(R.string.str_userMail),"error"), Context.MODE_PRIVATE);
PreferenceManager.setDefaultValues(getApplicationContext(),(sharedPrefsDefault.getString(getString(R.string.str_userMail),"error")), Context.MODE_PRIVATE, R.xml.preferences, false);
This is what I'm doing in onCreate to store the defaultPrefs
into my sharedPrefs
(user).
Solution
I'm not a hundred percent sure on what you're planning to achieve but create a SharedPreferences class of your own. Something like this.
public class UserPreferences{
SharedPreferences.Editor editor;
SharedPreferences sharedPreferences;
public UserPrefs(String userName, Context context) {
this.context = context;
sharedPreferences = context.getSharedPreferences(userName, 0);
editor = sharedPreferences.edit();
}
public String getUserMail(){
return sharedPreferences.getString("userMail","[email protected]");
}
public String setUserMail(String s){
editor.putString("userMail",s);
editor.apply();
}
}
Answered By - birukhimself
Answer Checked By - Timothy Miller (JavaFixing Admin)