Supposed I had 2 sharedPreferences files, one was created via "PreferenceManager.getDefaultSharedPreferences(...);" and one via getSharedPreferences(someString, Context.MODE_PRIVATE)", and I know that the keys are unique in both.
What would it take to move all keys&values from one file, to the other?
Is there an easy way to move them all?
I was thinking of using code similar to this answer, but won't I need to consider the type of each value there?
EDIT:
This is one way to do it, but it's a bit unsafe, as new types might be added to SharedPreferences in the future, which means it won't be supported:
final Editor edit = targetSharedPreferences.edit();
final Map<String, ?> srcSharedPreferenceAll = srcSharedPreference.getAll();
for (final Entry<String, ?> entry : srcSharedPreferenceAll.entrySet()) {
if (entry.getValue() instanceof Integer)
edit.putInt(entry.getKey(), (Integer) entry.getValue());
else if (entry.getValue() instanceof Boolean)
edit.putBoolean(entry.getKey(), (Boolean) entry.getValue());
else if (entry.getValue() instanceof Long)
edit.putLong(entry.getKey(), (Long) entry.getValue());
else if (entry.getValue() instanceof String)
edit.putString(entry.getKey(), (String) entry.getValue());
else if (entry.getValue() instanceof Float)
edit.putFloat(entry.getKey(), (Float) entry.getValue());
else if (entry.getValue() instanceof Set<?>)
edit.putStringSet(entry.getKey(), (Set<String>) entry.getValue());
}
edit.apply();
srcSharedPreference.edit().clear().apply();
I've succeeded copying the xml tags from one SharedPreference file to the other, but for some reason, it only get noticed by the SharedPreference object on the next session, and not the current one (probably it's cached). Here's the code:
private static final String DST_XML_FILE = "dst";
// creating the data, for reading&writing later:
final SharedPreferences srcSharedPreference = getSharedPreferences(String , Context.MODE_PRIVATE);
srcSharedPreference.edit().clear().putInt("someInt", 12).putString("someString", "hi").putBoolean("someBoolean", true).apply();
final SharedPreferences targetSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
targetSharedPreferences.edit().clear().putInt("someInt2", 23).putString("someString2", "hi there").putBoolean("someBoolean2", false).apply();
//checking content before
Log.d("AppLog", "before");
final Map<String, ?> src = srcSharedPreference.getAll();
Log.d("AppLog", "src:" + src.size());
final Map<String, ?> targetSharedPreferencesAll = targetSharedPreferences.getAll();
Log.d("AppLog", "target:" + targetSharedPreferencesAll.size());
// the moving of the XML tags:
final PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
File dstFile = new File(packageInfo.applicationInfo.dataDir, "shared_prefs/" + (VERSION.SDK_INT >= VERSION_CODES.N ? PreferenceManager.getDefaultSharedPreferencesName(this) : getPackageName() + "_preferences") + ".xml");
File srcFile = new File(packageInfo.applicationInfo.dataDir, "shared_prefs/" + SRC_XML_FILE + ".xml");
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
Node srcNode = docFactory.newDocumentBuilder().parse(srcFile).getFirstChild();
Node dstNode = docFactory.newDocumentBuilder().parse(dstFile).getFirstChild();
final NodeList childNodes = srcNode.getChildNodes();
for (int i = 0; i < childNodes.getLength(); ++i) {
final Node item = childNodes.item(i);
Node importedNode = dstNode.getOwnerDocument().importNode(item, true);
srcNode.removeChild(item);
dstNode.appendChild(importedNode);
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(dstNode);
StreamResult result = new StreamResult(dstFile);
transformer.transform(source, result);
//checking content after
Log.d("AppLog", "after");
final Map<String, ?> src = srcSharedPreference.getAll();
Log.d("AppLog", "src:" + src.size());
final Map<String, ?> targetSharedPreferencesAll = targetSharedPreferences.getAll();
Log.d("AppLog", "target:" + targetSharedPreferencesAll.size());