如何在重新安装应用程序(Android)后保存共享首选项



清单:

<application
android:fullBackupContent="@xml/backup_rules"
tools:replace="android:fullBackupContent">

我也尝试这个变体:

<application
android:fullBackupContent="@xml/backup_rules"
tools:replace="android:fullBackupContent"
android:allowBackup="true"
android:fullBackupOnly="true">

备份规则:

<?xml version="1.0" encoding="utf-8"?>
<full-backup-content>
<include domain="file" path="."/>
<include domain="database" path="."/>
<include domain="sharedpref" path="."/>
<include domain="external" path="."/>
<include domain="root" path="."/>
<exclude domain="sharedpref" path="EXPONEA_PUSH_TOKEN.xml"/>
</full-backup-content>

问题是,我试图将字符串保存到共享首选项中,并在重新安装应用程序后获得它,但出现了问题。

例如:

val namePreferences: SharedPreferences = getSharedPreferences("name", Context.MODE_PRIVATE)
namePreferences.edit().putString("name_test", user.name).apply()
val test = namePreferences.getString("name_test", "empty name")
//here test isn't empty

当我重新安装我的应用程序时:

val test = namePreferences.getString("name_test", "empty name")
//here test is empty ("empty_name")

卸载应用程序时,会删除所有应用程序文件夹:apk、数据库和SharedPreferences。如果要在重新安装后读取值,则必须将其存储在本地存储中。

最新更新