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.
...or clone the GitHub repository from the command line by using the following command:
$ git clone https://github.com/googlecodelabs/android-backup-codelab
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.
android-backup-codelab-master
zip file, unzip the file.android-backup-codelab
folder to see the backup-codelab-start
folder. manifests/AndroidManifest.xml
from the Project pane<application>
element. allowBackup="true"
. 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 ‘foo@example.com` 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.
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 backs up the app's data once a day to the user's Google Drive storage.
Complete the following steps to create backup:
$ adb shell bmgr enabled Backup Manager currently enabled $ adb shell bmgr list transports android/com.android.internal.backup.LocalTransport * com.google.android.gms/.backup.BackupTransportService
$ adb shell bmgr backupnow com.googlecodelabs.example.backupexample Running incremental backup for 1 requested packages.
Running this command will force Auto Backup to back up your application immediately.
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
By uninstalling, you are able to confirm that restoring the backup on a new install restores your data.
# see all backup information (optional) $ adb shell dumpsys backup # or use grep to find the current token only $ adb shell dumpsys backup | grep "com.googlecodelabs.example.backupexample" -A 4 | grep Current Current: <hexadecimal token>
$ adb shell bmgr restore <token> com.googlecodelabs.example.backupexample Scheduling restore: Android SDK built for x86 restoreStarting: 0 packages restoreFinished: 0 done
Performing a restore operation kills your app.
bmgr backupnow
adb shell bmgr restore
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.
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:
manifests/AndroidManifest.xml
<application>
element and notice the lint warning.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"?>
<full-backup-content>
<!-- 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 -->
<exclude
domain="sharedpref"
path="com.googlecodelabs.example.backupexample.PREF_LOGIN_DETAILS.xml" />
<exclude
domain="sharedpref"
path="com.googlecodelabs.example.backupexample.PREF_APP_INFO.xml" />
</full-backup-content>
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"?>
<full-backup-content>
<exclude
domain="sharedpref"
path="com.googlecodelabs.example.backupexample.PREF_LOGIN_DETAILS.xml" />
</full-backup-content>
You can specify multiple exclude elements for files:
domain="database"
. Android Studio scans your app for use of SQLiteHelper
and adds an exclude. <exclude domain="file" path="instant-run"/>
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.
$ adb shell bmgr backupnow com.googlecodelabs.example.backupexample Running incremental backup for 1 requested packages.
$ adb uninstall com.googlecodelabs.example.backupexample
# see all backup information (optional) $ adb shell dumpsys backup # or use grep to find the current token only $ adb shell dumpsys backup | grep "com.googlecodelabs.example.backupexample" -A 4 | grep Current Current: <hexadecimal token>
and
$ adb shell bmgr restore <token> com.googlecodelabs.example.backupexample Scheduling restore: Android SDK built for x86 restoreStarting: 0 packages restoreFinished: 0 done
Notice that only the preferences namely PREF_APP_INFO.xml
was restored from the backup as intended.
<exclude>
and <include>
tags.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 LoginActivity.java
file to set the hint on the email AutoCompleteTextView
:
LoginActivity.java
in the Android Studio project.onCreate()
if (!PrefUtils.needsLogin(this)) {
startActivity(new Intent(this, SettingsActivity.class));
finish();
}
//******************* Implement Login Hinting *****************
//******************* End implement Login Hinting *************
PrefUtils
//******************* Implement Login Hinting *****************
String accountHint = PrefUtils.getLoginHint(this);
if (accountHint != null) {
mEmailView.setHint(accountHint);
}
//******************* End implement Login Hinting *************
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.
You can use the following APIs to improve the restore user experience for your apps:
To learn more about Auto Backup, see the following resources:
All rights reserved. Java is a registered trademark of Oracle and/or its affiliates.