Here is a version that also supports row sets.
public static void copySharedPreferences(SharedPreferences fromPreferences, SharedPreferences toPreferences) { copySharedPreferences(fromPreferences, toPreferences, true); } public static void copySharedPreferences(SharedPreferences fromPreferences, SharedPreferences toPreferences, boolean clear) { SharedPreferences.Editor editor = toPreferences.edit(); if (clear) { editor.clear(); } copySharedPreferences(fromPreferences, editor); editor.commit(); } @TargetApi(Build.VERSION_CODES.HONEYCOMB) @SuppressWarnings({"unchecked", "ConstantConditions"}) public static void copySharedPreferences(SharedPreferences fromPreferences, SharedPreferences.Editor toEditor) { for (Map.Entry<String, ?> entry : fromPreferences.getAll().entrySet()) { Object value = entry.getValue(); String key = entry.getKey(); if (value instanceof String) { toEditor.putString(key, ((String) value)); } else if (value instanceof Set) { toEditor.putStringSet(key, (Set<String>) value);
Oliver jonas
source share