I've set up the Android Backup Service in my app using a custom class that extends BackupAgentHelper ... it basically looks like this:
public class MyBackups extends BackupAgentHelper {
@Override
public void onCreate() {
Log.d("MyBackups", "creating backup class");
this.addDefaultHelper();
String defaultSharedPrefsName = this.getPackageName() + "_preferences";
SharedPreferencesBackupHelper defaultPrefsHelper = new SharedPreferencesBackupHelper(this, defaultSharedPrefsName);
this.addHelper("default_prefs", defaultPrefsHelper);
}
@Override
public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState) throws IOException {
Log.d("MyBackups", "backing up " + data);
super.onBackup(oldState, data, newState);
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) throws IOException {
Log.d("MyBackups", "restoring");
super.onRestore(data, appVersionCode, newState);
// post-processing code goes here
}
}
I have this registered in the manifest file, and if I delete and reinstall the app, it runs as expected, with all the log messages appearing.
However, if I manually request a restore, like this...
BackupManager backupManager = new BackupManager(getApplicationContext());
int error = backupManager.requestRestore(
new RestoreObserver() {
public void restoreStarting(int numPackages) {
Log.d("MyBackups", "restoreStarting");
}
public void restoreFinished(int error) {
Log.d("MyBackups", "restoreFinished");
}
public void onUpdate(int nowBeingRestored, String currentPackage) {
Log.d("MyBackups", "onUpdate");
}
}
);
Log.d("MyBackups", "requestRestore result: " + error);
...restoreStarting and restoreFinished are called, and the error result is 0, but none of the BackupAgentHelper methods are called -- the "creating backup class" and "restoring" logs don't appear, and my post-processing code doesn't run. It seems as if a manual requestRestore bypasses my custom BackupAgentHelper subclass.
Is there anything else I need to hook up to make a manual restore work the same way as an automatic restore? Have you tried this and is it working for you?