Users invest a lot of time customizing settings in their apps. Restoring settings data for users when they upgrade to a new device is an important aspect of ensuring a great user experience. It's therefore important to minimize the number of steps that a user has to go through to pick up where they left off on their previous device. You can use Auto Backup to restore settings data even if a user doesn't log in to your app.

To restore an existing user's settings data on a new device, make sure you backup the following user settings:

Auto Backup is available on devices running Android 6.0 (API 23) and higher.

What you'll learn

What you'll need

How will you use this tutorial?

Read it through only Read it and complete the exercises

How would rate your experience with building Android apps?

Novice Intermediate Proficient

Download ZIP

...or clone the GitHub repository from the command line by using the following command:

$ git clone

Frequently asked questions

The first step is to turn on Auto Backup within your app. Turning on Auto Backup is controlled via an attribute named allowBackup on the <application> element.

  1. If you downloaded the android-backup-codelab-master zip file, unzip the file.
  2. Open the android-backup-codelab folder to see the backup-codelab-start folder.
  3. Open the sample using File -> New -> Import Project and select the backup-codelab-start.
  4. Open manifests/AndroidManifest.xml from the Project pane
  5. Navigate to the <application> element.
  6. Ensure that allowBackup="true".
  7. Click the Run button, and choose an emulator, which must be capable of running Android Oreo (API level 26). The login screen should appear.

Frequently asked questions

Log in to sample app

Let's take a detour from implementing Auto Backup and understand the sample app, which includes a main login screen that accepts a username and a password. You can imagine a real world app accepting other forms of login such as Twitter, facebook or Google. You will implement login hinting in this code lab to remind users what email address they last used to log in.

After you launch the example app, log in with username ‘` and password `hello`. The example app will save your login in shared prefs. In a real world app, the auth key would be derived by sending the username and password to the server.

Examine Shared Prefs

To simplify this codelab, the example app shows the state of shared preferences on the login screen and when logged in. You can see all three shared pref values in the debug text visible in-app.

In this step, we look at how to manually create and restore a backup of an app for testing purposes. We also examine the contents of the backup. After a user installs your app, the system performs a backup of the app once a day to the user's Google Drive storage.

Complete the following steps to create backup:

  1. Run the following command from a terminal on your desktop or machine:
$ adb backup com.googlecodelabs.example.backupexample
Now unlock your device and confirm the backup operation...
  1. Click BACK UP MY DATA to accept the request to back up your emulator.This creates a file named backup.ab on the desktop or machine that issued the adb backup command.

Complete the following steps to manually restore the backup:

  1. Ensure that the app is uninstalled. You can uninstall the app inside the emulator or via adb. To uninstall via adb, issue the following command:
$ adb uninstall com.googlecodelabs.example.backupexample
  1. Open Android Studio and Click Run to install the app to the emulator.

  1. Open a terminal and issue the adb restore <backup-file> command:
$ adb restore backup.ab
Now unlock your device and confirm the restore operation.
  1. Click RESTORE MY DATA to accept the restore operation on the device. Use the password from creating the backup to complete the restore.

What we covered in this section

In the previous section, we saw how to manually back up and restore your app data. In this section we'll first examine the data that was restored and then customize it to only include specific files and values.

Checking the data restored from a backup

After the data is restored, open the app on the emulator and notice that the Login screen is not shown at all. This is because all the data from the app was backed up as part of the adb backup operation.

In a real application, you should not backup sensitive information like an access token. To configure our application to exclude some data we will create an exclude.

In the case of the sample app, we want the user to log in to the app but we can aid the user by hinting about the login provider or email address that was last used to log in to the app. This suggests that we should only backup com.googlecodelabs.example.backupexample.PREF_APP_INFO.xml which contains our hint, and not PREF_LOGIN_DETAILS.xml which contains our username and access token.

Auto Backup lets you configure the specific files that should either be included or excluded from a backup. By default, all files in your data folder are backed up. To configure it you use the include and exclude rules specified in a descriptor declared in the android:fullBackupContent attribute of the <application> element.

Complete the following steps to create the backup descriptor:

  1. Open manifests/AndroidManifest.xml
  2. Navigate to the <application> element and notice the lint warning.

  1. Use Opt-Enter (Alt-Enter on windows) to select one of the quick-fixes.
  2. Click Set fullBackupContent attribute and generate descriptor. This option in studio examines the entire app and populates a list of shared preferences and database files within the descriptor.
  3. Examine the opened descriptor res/xml/backup_descriptor.xml and modify it to suit your needs.

The generated file looks as follows:

<?xml version="1.0" encoding="utf-8"?>
   <!-- TODO Remove the following "exclude" elements to make them a part of the auto backup -->
   <!-- Exclude the shared preferences file that contains the GCM registrationId -->
       path="com.googlecodelabs.example.backupexample.PREF_LOGIN_DETAILS.xml" />
       path="com.googlecodelabs.example.backupexample.PREF_APP_INFO.xml" />

Since we only need to exclude the PREF_LOGIN_DETAILS.xml, we keep that exclude and remove the other exclude for PREF_APP_INFO.xml. After editing our file should look as follows:

<?xml version="1.0" encoding="utf-8"?>
       path="com.googlecodelabs.example.backupexample.PREF_LOGIN_DETAILS.xml" />

You can specify multiple exclude elements for files:

The backup descriptor alternatively also lets you include specific files that should be part of the backup. If you specify an <include> element, the system no longer includes any files by default and backs up only the files specified. To include multiple files, use multiple <include> elements.

Finally we follow the steps outlined in "Manually creating and restoring from a backup" to see the contents of the backup.

  1. From your desktop, run:
adb backup com.googlecodelabs.example.backupexample
  1. Click BACK UP MY DATA to accept the request to back up the data from the app.
  2. Uninstall the app from the emulator by issuing the following command (or alternatively clear the data of the app):
adb uninstall com.googlecodelabs.example.backupexample
  1. Click Run in Android Studio to install the app on the emulator.
  2. Run the following command to restore the backup:
adb restore backup.ab
  1. Click RESTORE MY DATA, to accept the request to restore the app data from backup.

Notice that only the preferences namely PREF_APP_INFO.xml was restored from the backup as intended.

What we learnt in this section

In this section, we look at how to help users of your app after they've migrated to a new device. Instead of showing just a Login screen to the user, we can remind them of the provider they used for logging into the app the last time. For instance, your app could be using one of multiple providers such as Login with Twitter, Facebook, Google or just an email address.

Complete the following steps to modify the file to set the hint on the email AutoCompleteTextView:

  1. Open in the Android Studio project.
  2. Navigate to the section for implementing Login Hinting in onCreate()
if (!PrefUtils.needsLogin(this)) {
   startActivity(new Intent(this, SettingsActivity.class));

//******************* Implement Login Hinting *****************

//******************* End implement Login Hinting *************
  1. In the marked section add code to get the masked login hint from PrefUtils
//******************* Implement Login Hinting *****************
String accountHint = PrefUtils.getLoginHint(this);
if (accountHint != null) {
//******************* End implement Login Hinting *************
  1. Let's finally see this in action. Since we already restored the backup that contains the PREF_APP_DETAILS shared preference, we simply have to install the updated version of our code.

If your app were using other login providers, you would similarly store the provider name in a backed up shared preference and show the provider login as the first option to make it easier for users to login.

Congratulations, you finished! You're now ready to configure Auto Backup for your apps.

What we've covered

Next Steps

You can use the following APIs to improve the restore user experience for your apps:

Learn More

To learn more about Auto Backup, see the following resources:

All rights reserved. Java is a registered trademark of Oracle and/or its affiliates.