This week's book giveaway is in the Java in General forum.
We're giving away four copies of Java by Comparison (eBook) and have Simon Harrer, Jörg Lenhard, Linus Dietz on-line!
See this thread for details.
Win a copy of Java by Comparison (eBook) this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

using the BackupAgentHelper to backup AND restore a string?  RSS feed

Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • 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 .....(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="" 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!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!