SharedPreferencesBackupHelper.javaAPI DocAndroid 5.1 API5157Thu Mar 12 22:22:10 GMT


public class SharedPreferencesBackupHelper extends FileBackupHelperBase implements BackupHelper
A helper class that can be used in conjunction with {@link} to manage the backup of {@link android.content.SharedPreferences}. Whenever a backup is performed, it will back up all named shared preferences that have changed since the last backup operation.

To use this class, the application's backup agent class should extend {@link}. Then, in the agent's {@link BackupAgent#onCreate()} method, an instance of this class should be allocated and installed as a backup/restore handler within the BackupAgentHelper framework. For example, an agent supporting backup and restore for an application with two groups of {@link android.content.SharedPreferences} data might look something like this:


public class MyBackupAgent extends BackupAgentHelper {
// The names of the SharedPreferences groups that the application maintains. These
// are the same strings that are passed to {@link Context#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);

No further implementation is needed; the {@link BackupAgentHelper} mechanism automatically dispatches the {@link BackupAgent#onBackup(android.os.ParcelFileDescriptor, BackupDataOutput, android.os.ParcelFileDescriptor) BackupAgent.onBackup()} and {@link BackupAgent#onRestore(BackupDataInput, int, android.os.ParcelFileDescriptor) BackupAgent.onRestore()} callbacks to the SharedPreferencesBackupHelper as appropriate.

Fields Summary
private static final String
private static final boolean
private android.content.Context
private String[]
Constructors Summary
public SharedPreferencesBackupHelper(android.content.Context context, String prefGroups)
Construct a helper for backing up and restoring the {@link android.content.SharedPreferences} under the given names.

context The application {@link android.content.Context}
prefGroups The names of each {@link android.content.SharedPreferences} file to back up


        mContext = context;
        mPrefGroups = prefGroups;
Methods Summary
public voidperformBackup(android.os.ParcelFileDescriptor oldState, BackupDataOutput data, android.os.ParcelFileDescriptor newState)
Backs up the configured {@link android.content.SharedPreferences} groups.

        Context context = mContext;

        // If a SharedPreference has an outstanding write in flight,
        // wait for it to finish flushing to disk.

        // make filenames for the prefGroups
        String[] prefGroups = mPrefGroups;
        final int N = prefGroups.length;
        String[] files = new String[N];
        for (int i=0; i<N; i++) {
            files[i] = context.getSharedPrefsFile(prefGroups[i]).getAbsolutePath();

        // go
        performBackup_checked(oldState, data, newState, files, prefGroups);
public voidrestoreEntity(BackupDataInputStream data)
Restores one entity from the restore data stream to its proper shared preferences file store.

        Context context = mContext;
        String key = data.getKey();
        if (DEBUG) Log.d(TAG, "got entity '" + key + "' size=" + data.size());

        if (isKeyInList(key, mPrefGroups)) {
            File f = context.getSharedPrefsFile(key).getAbsoluteFile();
            writeFile(f, data);