Backup / restore general android settings

I am trying to backup / restore the general settings of my application, I performed this step using the Android backup service:

In Manifest.xmlin tag<application>

<meta-data android:name="com.google.android.backup.api_key" android:value="My Key" />

added this class:

public class MyBackupAgent extends BackupAgentHelper {

    // The name of the SharedPreferences file
    static final String PREFS = "my_preferences";


    @Override
    public void onCreate() {
        SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(this, PREFS);
        addHelper(Utilities.SETTINGS_KEY, helper);
    }

}

when the set value is for general preference I do this:

BackupManager backupManager = new BackupManager(context);
backupManager.dataChanged();

But if I uninstall / reinstall the application, the changes are not applied ...

+4
source share
3 answers

My guess: you forgot to add

android:allowBackup="true"

inside the tag <application>in the AndroidManifest.xml file

+2
source

:

    @Override
public void onCreate() {
    FileBackupHelper helper = new FileBackupHelper(this,
            TOP_SCORES, PLAYER_STATS);
    addHelper(FILES_BACKUP_KEY, helper);
}

onBackup() onRestore(). . : https://developer.android.com/guide/topics/data/keyvaluebackup.html#BackupAgentHelper

+2

dataChanged(), , - , , Wi-Fi. " reset", " ".

, ( ), context.getSharedPreferences("my_preferences", Context.MODE_PRIVATE);

+1

Source: https://habr.com/ru/post/1679909/


All Articles