Do you want to know the real first open of your application that persists across app uninstalls or when the user moves to a new phone? Or do you want to save any settings of the application (like Disk usage settings, Sync settings, etc) without losing it when the app is reinstalled?
The first answer that comes to mind is to create a web service that stores the data and a login process by email or phone number. But it’s hard to implement for the developer and even user to login for just some sort of insensitive data.
And maybe you are just familiar with Android development. Then what?
There is an easy way to solve this problem: Android Auto Backup
Warning : Any sensitive user data sould be kept secure with passwords and pin codes and Auto backup are not good for this type of data. For sensitive data You can use Google Sign-In or create your own webservice.
So I will describe the Auto Backup API that is useful for simple usages, But for complex usages, you can use Android backup Service.
Auto backup is a file-based backup service. This means it can save your Shared Preferences files, Database files, and any other types of file. But be aware that each app has a 25MB of data limit per user/device.
Auto backup is enabled by default and it saves near to any file that is saved to apps private directory except the files in the cache directories. To disable the auto-backup set the
android:allowBackupattribute to false in the
<application> element of your manifest file:
<manifest ... >
<application android:allowBackup="false" ... >
Backups occur automatically when all of the following conditions are met:
- The user has enabled backup on the device. In Android 9, this setting is in Settings > System > Backup. (It’s enabled by default)
- At least 24 hours have elapsed since the last backup.
- The device is idle.
- The device is connected to a Wi-Fi network (if the device user hasn’t opted in to mobile-data backups).
Include and exclude files
This section shows you how to define custom XML rules to control what gets backed up.
android:fullBackupContentattribute to the
<application>element. This attribute points to an XML file that contains backup rules.
2. Create the
back_rules.xml file in the
res/xml/ directory and add include/exclude rules as desired to it:
<?xml version="1.0" encoding="utf-8"?>
<include domain="sharedpref" path="."/>
<exclude domain="sharedpref" path="excluded_shared_pref.xml"/>
The above declaration adds all files in the SharedPreferences directory except
The syntax of the
fullBackupContent XML file is like below:
<include domain=["file" | "database" | "sharedpref" | "external" | "root"]
requireFlags=["clientSideEncryption" | "deviceToDeviceTransfer"] />
<exclude domain=["file" | "database" | "sharedpref" | "external" | "root"]
exclueelements both have
domain attribute you can specify the directory of the file like below:
sharedprefpoints to the directory where
filepoints to the directories returned by
getDatabasePath()where database files are stored.
externalpoints to the external files directory returned by
rootpoints to the directory on the filesystem where all private files belonging to the app are stored.
path attribute you can specify the file or folder to include or exclude.
requireFlags here for brevity. You can read more about it in this guide.
So if you want to save a SharedPreferences or Database file, write an
inclue rule, set it’s
database respectively and set the relative path of the file in the
path attribute. The relative path is the file path related to where the domain path specifies, but usually, it’s just the file name(eg.,
Note that if you specify an <include> element, the system no longer includes any files by default and backs up only the files specified.
The backup will get saved in users google drive And it has a 25MB storage limit per app/user. The backup file will not count on the user’s google drive storage limit.
The restore will happen when the app is installed on a device with the same google drive account(same Gmail). So when the app is launched for the first time its files are restored.
To make sure your implementation works properly you can force the android device to backup files with the help of an
First, work with the app in order for the data you want to test to get created. Then force the device to make a backup by typing this command in a command-line:
(I tested this command with an actual device but it may work with an emulator too)
adb shell bmgr backupnow <Replace Your App's PACKAGENAME here>
Then just reinstall the app and it should have the data you want. Read this guide to know more about testing.
Happy coding. 😍