8

I am implementing the new BackupAgentHelper from Android OS 2.2.

For it to work you need to supply the name of the SharedPreferences that you want to backup:

public class MyPrefsBackupAgent extends BackupAgentHelper {
// The name of the SharedPreferences file
static final String PREFS = "user_preferences";

// 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
void onCreate() {
    SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(this, PREFS);
    addHelper(PREFS_BACKUP_KEY, helper);
}
}

Problem is I use the PreferenceManager.getDefaultSharedPreferences() method in my application to get the default shared preferences instance. Now I need to supply the name of that file. How can I find out what name that is?

(And why do they not supply a helper that just does a DefaultSharedPreferences backup?)

HitOdessit
  • 7,198
  • 4
  • 36
  • 59
Peterdk
  • 15,625
  • 20
  • 101
  • 140

1 Answers1

16

It looks like it's called "packagename_preferences"

Peterdk
  • 15,625
  • 20
  • 101
  • 140
  • 2
    Yes, if you browse /data/data//shared_prefs you can see the files used for SharedPreferences. The name you need is indeed _preferences. Also, I created an issue to ask for a more convenient way to do this for "default" SharedPreferences: http://code.google.com/p/android/issues/detail?id=11922. (I agree with you, that should be there.) – Charlie Collins Oct 17 '10 at 12:23
  • How to backup data set with a PreferenceActivity? –  May 31 '11 at 11:42
  • 1
    If you set a breakpoint at `prefs = PreferenceManager.getDefaultSharedPreferences(this);` you can look at mBackupFile for the xml file name. – Chuck D Oct 02 '12 at 13:07