Monday, 19 July 2010

backup getDefaultSharedPreferences

If you use androids preference framework then you are likely to also use the getDefaultSharedPreferences to store your preferences. And of course you want to backup your preferences as well. Only problem is that SharedPreferencesBackupHelper need the name of your preferences and getDefaultSharedPreferencesName is private.

Not to force you to read the source code, the correct code is:



public class Calculator_Backup
extends
android.app.backup.BackupAgentHelper
{



final android.app.backup.SharedPreferencesBackupHelper helper =
new android.app.backup.SharedPreferencesBackupHelper (
/* context => */this,
/* prefGroups => */this.getPackageName () + "_preferences");

this.addHelper (
/* keyPrefix => */Calculator_Backup.PREFERENCES_BACKUP_KEY,
/* helper => */helper);


Calculator_Backup need to be in the same package as your shared preferences.

No comments:

Post a Comment