I've researched and followed how to get my android app to backup the data to google backup so if user loses phone or upgrades to a new phone, they don't lose their data. However, when I test it out (by using the app myself, then uninstalling and reinstalling), no data is restored. Here's what I've done. Perhaps someone can figure out what is wrong.

  1. Applied for a backup key from google
  2. Placed following code in Manifest File (in place of key I did add the key value and for packageName I used my app package name)

    android:backupAgent="packageName.MyPrefsBackup">
  3. Created class MyPrefsBackup with following code. The name of the sharedpreference file I want to backup is called UserDB. As far as the PREFS_BACKIP_KEY, I just called it prefs. From what I understand, this is not the same key as the one that goes in the manifest file.

Code:

package packageName;import android.app.backup.BackupAgentHelper;import android.app.backup.SharedPreferencesBackupHelper;p ublic class MyPrefsBackup extends BackupAgentHelper { // The name of the SharedPreferences file static final String PREFS = "UserDB"; // A key to uniquely identify the set of backup data static final String PREFS_BACKUP_KEY = "prefs"; // Allocate a helper and add it to the backup agent public void onCreate() { SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(this, PREFS); addHelper(PREFS_BACKUP_KEY, helper); }}
  1. Added BackupManager mBackupManager = new BackupManager(this); in my main class where I call the backup manager in next step
  2. Lastly, in my main program I call the backupHelper when data is changed by the following line:

    mBackupManager.dataChanged();

Any help is much appreciated.


Check Solution