Make preferences safe across multiple processes

This commit is contained in:
Dominik Schürmann 2014-09-08 11:30:31 +02:00
parent f4592c8b95
commit 8ab2491645

View file

@ -47,11 +47,18 @@ public class Preferences {
public static synchronized Preferences getPreferences(Context context, boolean forceNew) { public static synchronized Preferences getPreferences(Context context, boolean forceNew) {
if (sPreferences == null || forceNew) { if (sPreferences == null || forceNew) {
sPreferences = new Preferences(context); sPreferences = new Preferences(context);
} else {
// to make it safe for multiple processes, call getSharedPreferences everytime
sPreferences.updateSharedPreferences(context);
} }
return sPreferences; return sPreferences;
} }
private Preferences(Context context) { private Preferences(Context context) {
updateSharedPreferences(context);
}
public void updateSharedPreferences(Context context) {
// multi-process preferences // multi-process preferences
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
mSharedPreferences = context.getSharedPreferences("APG.main", Context.MODE_MULTI_PROCESS); mSharedPreferences = context.getSharedPreferences("APG.main", Context.MODE_MULTI_PROCESS);