Android SharedPreferences Backup Not Working

后端 未结 1 1199
灰色年华
灰色年华 2021-01-02 18:32

I\'ve been doing my homework on how to backup SharedPreferences in my Android application, especially using reflection to maintain backwards compatibility. At least I\'ve be

相关标签:
1条回答
  • 2021-01-02 19:13

    In your main calling activity (first one that starts in your app), you need to instantiate BackupManager:

    BackupManager mBackupManager = new BackupManager(getApplicationContext());
    

    This will tell the backupmanager to look for the backup file and load it.

    You need to make sure the preferences file is format "packagename_preferences" eg. andy_preferences. And use the same name when you first saved your preferences. (Very important!)

    After the your settings activity saves via apply() or commit(), you need to tell BackupManager that something has changed so include immediately after:

    mBackupManger.dataChanged();
    
    0 讨论(0)
提交回复
热议问题