Make your users happy, (please) use the BackupManager

Every developer wants to make they users happy. This way the users keep using their application, or better yet, keep buying.

Is there anything more frustrating for a user than losing all ‘three star’ phases of his favorite game?
Or have to fill in lots of data fields after a new installation of an application that he uses, in his new and recently acquired ‘latest generation smartphone’?

Android provides a very simple feature to back up settings and preferences on the Cloud, the BackupManager

Using the BackupManager you can backup your SharedPreferences and configuration files on the cloud (do not use it for large binary files).

And the best of all is that it is very simple to use:

  • First you need to register an API KEY at http://code.google.com/android/backup/signup.html
  • Then we need to create a class that extends the BackupAgentHelper:
    import android.app.backup.BackupAgentHelper; 
    import android.app.backup.SharedPreferencesBackupHelper; 
    
    public class MyBackupAgent extends BackupAgentHelper {
        // The names of the SharedPreferences groups that the application maintains.  These
        // are the same strings that are passed to getSharedPreferences(String, int). 
        static final String PREFS_DISPLAY = "displayprefs"; 
        static final String PREFS_SCORES = "highscores"; 
    
        // An arbitrary string used within the BackupAgentHelper implementation to
        // identify the SharedPreferenceBackupHelper's data. 
        static final String MY_PREFS_BACKUP_KEY = "myprefs"; 
    
        // Simply allocate a helper and install it 
        void onCreate() { 
            SharedPreferencesBackupHelper helper = 
                    new SharedPreferencesBackupHelper(this, PREFS_DISPLAY, PREFS_SCORES);
            addHelper(MY_PREFS_BACKUP_KEY, helper); 
        } 
    }
  • On yout AndroidManifest.xml tag add android:allowBackup=”true” to enable the backup and android:BackupAgent=”MyBackupAgent” to tell what your BackupAgent is.
    Still on the application tag, add:

    <meta-data android:name="com.google.android.backup.api_key" 
            android:value="YOUR API KEY HERE" />
  • Each time you change some data in the SharedPreferences groups you want to backup (in our example “displayprefs” and “highscores”) you need to call DataChanged() on an instance of BackupManager:
    BackupManager backupManager = new BackupManager(context);
    ... 
    backupManager.dataChanged();

Okay, that’s all you need to back up and restore settings in/from the cloud and especially keep your users happy.

So, if you are a developer, make your users happy, and if you are a user, ask the developers to make you happy.

The BackupManager is available from android 2.2 froyo.

Sample source code available at BackupManagerTest

Leave a comment