Search code examples
androidandroid-backup-service

SharedPreferencesBackupHelper auto restore doesn't work


I'm trying to use SharedPreferencesBackupHelper to save my SharedPreferences value to cloud.

AndroidManifest.xml

<application
    android:allowBackup="true"
    android:backupAgent=".DataBackupAgent"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <meta-data android:name="com.google.android.backup.api_key" android:value="AEdPqrEAAAAIXMH86OqosQlXYuS0QbfyOaZT8fUadY1QUDzo2w" />

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

DataBackupAgent.java:

public class DataBackupAgent extends BackupAgentHelper {

public static final String PREFS = "data_prefs";
public static final String PREFS_BACKUP_KEY = "myprefs";

@Override
public void onCreate() {
    SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(this, PREFS);
    addHelper(PREFS_BACKUP_KEY, helper);
}

}

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    backupManager = new BackupManager(this);
    prefs = getSharedPreferences(DataBackupAgent.PREFS, Context.MODE_PRIVATE);
    edit = prefs.edit();

    text = (EditText)findViewById(R.id.editText);
    String value = prefs.getString(DataBackupAgent.PREFS_BACKUP_KEY,"");
    text.setText(value);

    Button btnBackup = (Button)findViewById(R.id.button);
    btnBackup.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            edit.putString(DataBackupAgent.PREFS_BACKUP_KEY,text.getText().toString());
            edit.commit();
            backupManager.dataChanged();
        }
    });
}

My steps:

  1. Write something at the EditText, click Backup button
  2. Close app and open again. The saved value will be shown in EditText
  3. Uninstall the app and reinstall again. The saved value is not shown in EditText at all.

Edit at 27/02/2015:

I added the following code to restore manually:

    backupManager.requestRestore(new RestoreObserver() {
        @Override
        public void restoreFinished(int error) {
            super.restoreFinished(error);

            String value = prefs.getString(DataBackupAgent.PREFS_BACKUP_KEY,"");
            text.setText(value);
        }

        @Override
        public void restoreStarting(int numPackages) {
            super.restoreStarting(numPackages);
        }

        @Override
        public void onUpdate(int nowBeingRestored, String currentPackage) {
            super.onUpdate(nowBeingRestored, currentPackage);
        }
    });

Unfortunately no callback functions are called.

This means back or auto restore doesn't work at all. Any idea? Thanks


Solution

  • My steps: 1. Write something at the EditText, click Backup button 2. Close app and open again. The saved value will be shown in EditText 3. Uninstall the app and reinstall again. The saved value is not shown in EditText at all.

    To test your implementation there are others steps related to the use of bmgras we can see here.

    Nevertheless I implemented this feature some days ago and following the steps in the documentation using a real device - Samsung SII - the automatic restore doesn't happen BUT using the emulator all was fine.

    Logcat will show you all the operation output details.

    IMO, the Android Data Backup feature is not reliable today. We can see some discussion about the implementation problems here and here.

    Hope it helps!