Backup API Implementation: Creating Backup of App Data and Settings Over Cloud

How about creating a backup setting for an android application, so that when the application is uninstalled the personal settings it does not get erased permanently, instead the settings is stored on the cloud and the next time when you install the application it asks you to restore the settings. Interestingly, it can restore the settings when you install the application into other device too. It can be achieved through Backup API provided by Google. You will find it really interesting, easy and useful.

It’s not for the synchronizing of the data with other apps or storing the data which could further be used as in the case of SQLite . Also there is no method to access the backed up data other than the API and neither any way to make the Backup status known. This makes it easier to implement and lesser code it requires.

Android currently supports and provides helpers to backup and restore the complete data of the Shared Preferences or the internal storage if they are light weighted i.e. less than one megabyte.

For the implementation of Backup API the first and the foremost thing which has to be done is create the API key for your application by clicking on the link
and registering your application by giving the appropriate package (the package name of your app)

Android Backup

Android Backup

An API Key would be generated and this API key has to be added into the AndroidManifest file like this

<meta-data android:name=””
android:value=”ABcDe1FGHij2KlmN3oPQRs4TUvW5xYZ” />

This meta data tag should be the direct child of the <application> tag in the AndroidManifest.xml file of the application. For the implementation of Backup API Basically there are two classes which will be used.



Talking about BackupAgentHelper class:

we create a MyBackupAgentHelper class which extends the wrapper class BackupAgentHelper and what you need to do is just add some code in the overridden method onCreate of the BackupAgentHelper class. The class looks somewhat like this.

public class MyBackUpAgentHelper extends BackupAgentHelper {

	// string constant declared to give the file name which has to be backed up
	private static final String FILENAME = "name_of_file";

	// backup key which will uniquely identify the backup data
	private static final String FILE_BACKUP_KEY = "backup_key";

	 * called when the backup has to be created
	 * @see
	public void onCreate() {

		//file backup helper class to backup the file
		FileBackupHelper backupHelper = new FileBackupHelper(this, FILENAME);
		addHelper(FILE_BACKUP_KEY, backupHelper);


here we use the FileBackupHelper class to create the backup of the files , if you want to more about this class you can visit this link.

and if you want to know more about the BackupAgentHelper class you can go through the following link

and the addHelper(FILE_BACKUP_KEY, backupHelper); method is used to add the BackupHelper class.

Similar to this if you want to backup the SharedPreferences data you need to use the SharedPreferenceBackupHelper class just as we used the FileBackupHelper class.


// to backup the shared preferences of the app

SharedPreferencesBackupHelper helper = new
                            SharedPreferencesBackupHelper(this, PREFS_DISPLAY, PREFS_DATA);
addHelper(MYPREF_BACKUP_KEY, helper);


where PREFS_DISPLAY, PREFS_DATA and MYPREF_BACKUP_KEY are the string constants containing the data of the shared preference and the key for  the shared preference data.

You can add as many BackupHelper instance as many you want in the BackupAgentHelper class but only you need to have one instance of the FileBackupHelper to back up the files in your app and one instance of the SharedPreferenceBackupHelper to back up the shared preferences of the application.

This is almost done but what is the use of the BackupManager class? If you want to request a backup you can use the instance of the BackupManager class and call its dataChanged(); method

	 * method to request for the backup
	private void requestBackup() {
		//instance of the BacupManager class
		BackupManager backupManager = new BackupManager(this);

		//calling its dataChanged method

This call notifies the backup manager that there is data ready to be backed up to the cloud. At some point in the future, the backup manager then calls your backup agent’s onBackup() method. You can make the call whenever your data has changed, without having to worry about causing excessive network activity. If you request a backup twice before a backup occurs, the backup only occurs once.

And if you want to know more about the class you can find it here

Tip for Testing :

You just need to implement the API as explained above and it will create backup but it may or may not create backup at the same time (as explained above) so in order to test the BackUpApi Implementation install and leave it installed for few hours and then try to reinstall it. Then it will ask you to restore the app data.

Thank you for reading 🙂

Written By: Trayambak Mishra, Software Developer, Mindfire Solutions

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s