How to save Shared preference after re-installing the app (Android)

Viewed 27

Manifest:

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

Also I try this variant:

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

Backup_rules:

<?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>

The problem is that I try to save string to shared preference and get it after re-installing the app, but something went wrong.

For example:

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

When I re-installing my app:

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

When you uninstall an app, all the app folder is deleted: apk, database and SharedPreferences. If you want to read a value after re-install you must store it in the Local storage instead.

Related