I need to keep a set of preferences per user and another set of preferences for the whole app. The users are kept in a SQLite database.
I will also be using PreferenceFragment to show the preferences.
If I use getSharedPreferences() per user and one for the system, will the preference fragments show the correct preferences?
Or should I use tables in my case and either:
What is the best practice in this case?
You can create Multiple Preference File which have Userid associated with it along with a prefix. Like :-
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
/**
* Created by vikas on 12/14/15.
*/
public class Pref {
private static final String PREF_NAME = "AppName_%s";
private SharedPreferences mPreferences;
private String mUserId;
private static Pref sInstance;
private Pref(Context pContext, String pUserId) {
mUserId = pUserId;
mPreferences = pContext.getSharedPreferences(String.format(PREF_NAME, pUserId), Context.MODE_PRIVATE);
}
public String getUserId() {
return mUserId;
}
public static Pref getPreferences(Context context, String userId) {
if (null != sInstance || !TextUtils.equals(sInstance.getUserId(), userId)) {
sInstance = new Pref(context, userId);
}
return sInstance;
}
//write methods for updating in preference files
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With