Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backup/restore shared preferences android

I'm trying to backup/restore shared preferences of my app, I followed this step using Android Backup Service:

In Manifest.xml in <application> tag

<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 set value to shared preference I do this:

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

But if I uninstall/reinstall app, changes doesn't apply...

like image 793
Michele Lacorte Avatar asked Jun 22 '17 18:06

Michele Lacorte


People also ask

Where is shared preference file stored Android?

Android Shared Preferences Overview Android stores Shared Preferences settings as XML file in shared_prefs folder under DATA/data/{application package} directory. The DATA folder can be obtained by calling Environment. getDataDirectory() .

Where are my Android backups stored?

Backup data is stored in a private folder in the user's Google Drive account, limited to 25MB per app. The saved data does not count towards the user's personal Google Drive quota. Only the most recent backup is stored. When a backup is made, the previous backup (if one exists) is deleted.

Can shared preferences be deleted?

SharedPreferences is what Android and iOS apps use to store simple data in an allocated space. This data exists even when the app is shut down and starts up again; we can still retrieve the value as it was. The data stored in SharedPreferences can be edited and deleted.

How to backup shared preference data in Android?

Since Android API 23 ( or Android “6.0” or Android “Marshmellow”) the BackupManager stores all the data of an application including the Shared preference on the cloud. Thus when your application is reinstalled, the shared preference data is restored. A key point here to note is that the process of backup is like a black-box.

How much data can I back up and restore on Android?

The amount of data is limited to 25MB per user of your app and there's no charge for storing backup data. Your app can customize the backup process or opt out by disabling backups. For an overview of Android's backup options and guidance about which data you should back up and restore, see the data backup overview.

Why can't my Android device restore from its own backup?

The device prioritizes its own backup if backups from both sources are available. If the user didn't go through the device setup wizard, then the device can restore only from its own backups. To simplify testing, Android includes tools that let you manually initiate a restore of your app.

What is shared preferences in Android?

Android - Shared Preferences. Android provides many ways of storing data of an application. One of this way is called Shared Preferences. Shared Preferences allow you to save and retrieve data in the form of key,value pair.


3 Answers

My guess is you forgot to add

android:allowBackup="true"

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

like image 60
amilcar-sr Avatar answered Sep 21 '22 15:09

amilcar-sr


You have to allocate a helper and add it to the backup agent like this:

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

and also consider overriding onBackup() and onRestore() methods. See explanation here: https://developer.android.com/guide/topics/data/keyvaluebackup.html#BackupAgentHelper

like image 28
Sagi Mymon Avatar answered Sep 20 '22 15:09

Sagi Mymon


when you call dataChanged() you just notify system that something is changed, it does not start backup in this moment, give it some time and wi fi connection. Check in your device's settings under 'Backup and reset' if 'automatic restore' is set.

Make sure that you writing to the same preferences (with the same key) which you are saving context.getSharedPreferences("my_preferences", Context.MODE_PRIVATE);

like image 39
RadekJ Avatar answered Sep 20 '22 15:09

RadekJ