• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

using the BackupAgentHelper to backup AND restore a string?

Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I have a sharedPreferences that I whant to save using the BackupAgentHelper and so on..

but i cant get this to work
(or maby I test this WRONG..)
I first get the string appSettings from my Preferences..
(this is in my activity)

appSettings = loadSavedPreferences();
System.out.println("appSettings is : " + appSettings );
try {
backupdata(appSettings );
} catch (Exception e) {

private String loadSavedPreferences() {
SharedPreferences sharedPreferences = PreferenceManager
String test = sharedPreferences.getString("appSettings", getString(R.string.emty_settingsifnew));
return test;

in the backupdata() i pas in a string;
(this is altso in the activity)

private void backupdata(String appSettings2) {
edit.putString("appSettings ", appSettings2 );
Log.d("BACKUP", "Calling backup...");

then in a other Class:

import android.app.backup.BackupAgentHelper;
import .....(and so on..)

public class MyBackupAgent extends BackupAgentHelper {

static final String PREFS_TEST = "testprefs";

static final String MY_PREFS_BACKUP_KEY = "myprefs";

private String appSettings;

// Simply allocate a helper and install it
public void onCreate() {
SharedPreferencesBackupHelper helper =
new SharedPreferencesBackupHelper(this, PREFS_TEST);
addHelper(MY_PREFS_BACKUP_KEY, helper);
Log.d("Test", "Adding backupagent...");


and in the manifest:
(the internet connection premision have i added..)

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

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

(the key have I registrate..)

please note :
I have NOt a account in "googledev app site ", I have only registrate on:

and I have tryed to read :

but i cant understand the problem..


when I run my app (with a internetconnection) thrue eclipse and a USBcable and do a settings and call "backupdata()"
and then uninstall the app.

and then reinstall the app (from eclipse to a mobile)
and run the app and try to do this:

backupManager = new BackupManager(this);
prefs = getSharedPreferences(MyBackupAgent.PREFS_TEST, Context.MODE_PRIVATE);
edit = prefs.edit();
appSettings = prefs.getString("appSettings", "");

i recive ""

and not the last settings :-(

do you have any ideas?

best regards to you all!
    Bookmark Topic Watch Topic
  • New Topic