Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [XML Attrs](https://developer.android.com/reference/android/app/backup/BackupManager#lattrs) \| [Ctors](https://developer.android.com/reference/android/app/backup/BackupManager#pubctors) \| [Methods](https://developer.android.com/reference/android/app/backup/BackupManager#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/app/backup/BackupManager#inhmethods)

# BackupManager

*** ** * ** ***


`
public


class
BackupManager
`


`

extends https://developer.android.com/reference/java/lang/Object


`

`


`

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.app.backup.BackupManager |

<br />

*** ** * ** ***

The interface through which an application interacts with the Android backup service to
request backup and restore operations.
Applications instantiate it using the constructor and issue calls through that instance.


When an application has made changes to data which should be backed up, a
call to `https://developer.android.com/reference/android/app/backup/BackupManager#dataChanged()` will notify the backup service. The system
will then schedule a backup operation to occur in the near future. Repeated
calls to `https://developer.android.com/reference/android/app/backup/BackupManager#dataChanged()` have no further effect until the backup
operation actually occurs.


A backup or restore operation for your application begins when the system launches the
`https://developer.android.com/reference/android/app/backup/BackupAgent` subclass you've declared in your manifest. See the
documentation for `https://developer.android.com/reference/android/app/backup/BackupAgent` for a detailed description
of how the operation then proceeds.


Several attributes affecting the operation of the backup and restore mechanism
can be set on the `
https://developer.android.com/guide/topics/manifest/application-element`
tag in your application's AndroidManifest.xml file.

### Developer Guides

For more information about using BackupManager, read the
[Data Backup](https://developer.android.com/guide/topics/data/backup) developer guide.

<br />

## Summary

| ### XML attributes ||
|---|---|
| [`android:allowBackup`](https://developer.android.com/reference/android/app/backup/BackupManager#attr_android:allowBackup) | Whether to allow the application to participate in the backup and restore infrastructure. |
| [`android:backupAgent`](https://developer.android.com/reference/android/app/backup/BackupManager#attr_android:backupAgent) | The name of the class subclassing `BackupAgent` to manage backup and restore of the application's data on external storage. |
| [`android:killAfterRestore`](https://developer.android.com/reference/android/app/backup/BackupManager#attr_android:killAfterRestore) | Whether the application in question should be terminated after its settings have been restored during a full-system restore operation. |
| [`android:restoreAnyVersion`](https://developer.android.com/reference/android/app/backup/BackupManager#attr_android:restoreAnyVersion) | Indicate that the application is prepared to attempt a restore of any backed-up dataset, even if the backup is apparently from a newer version of the application than is currently installed on the device. |

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/app/backup/BackupManager#BackupManager(android.content.Context)(https://developer.android.com/reference/android/content/Context context) ` Constructs a BackupManager object through which the application can communicate with the Android backup system. |

| ### Public methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/app/backup/BackupManager#dataChanged()() ` Notifies the Android backup system that your application wishes to back up new changes to its data. |
| ` static void` | ` https://developer.android.com/reference/android/app/backup/BackupManager#dataChanged(java.lang.String)(https://developer.android.com/reference/java/lang/String packageName) ` Convenience method for callers who need to indicate that some other package needs a backup pass. |
| ` https://developer.android.com/reference/android/os/UserHandle` | ` https://developer.android.com/reference/android/app/backup/BackupManager#getUserForAncestralSerialNumber(long)(long ancestralSerialNumber) ` Returns a `https://developer.android.com/reference/android/os/UserHandle` for the user that has `ancestralSerialNumber` as the serial number of the its ancestral work profile or `null` if there is none. |
| ` int` | ` https://developer.android.com/reference/android/app/backup/BackupManager#requestRestore(android.app.backup.RestoreObserver)(https://developer.android.com/reference/android/app/backup/RestoreObserver observer) ` *This method was deprecated in API level 28. Applications shouldn't request a restore operation using this method. In Android P and later, this method is a no-op.* *Restore the calling application from backup. The data will be restored from the current backup dataset if the application has stored data there, or from the dataset used during the last full device setup operation if the current backup dataset has no matching data. If no backup data exists for this application in either source, a non-zero value is returned.* *If this method returns zero (meaning success), the OS attempts to retrieve a backed-up dataset from the remote transport, instantiate the application's backup agent, and pass the dataset to the agent's `https://developer.android.com/reference/android/app/backup/BackupAgent#onRestore(android.app.backup.BackupDataInput,%20int,%20android.os.ParcelFileDescriptor)` method.* *Unlike other restore operations, this method doesn't terminate the application after the restore. The application continues running to receive the `https://developer.android.com/reference/android/app/backup/RestoreObserver` callbacks on the `observer` argument. Full backups use an `https://developer.android.com/reference/android/app/Application` base class while key-value backups use the application subclass declared in the AndroidManifest.xml `<application>` tag.* |

| ### Inherited methods |
|---|---|
| From class ` https://developer.android.com/reference/java/lang/Object ` |---|---| | ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/java/lang/Object#clone()() ` Creates and returns a copy of this object. | | ` boolean` | ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Indicates whether some other object is "equal to" this one. | | ` void` | ` https://developer.android.com/reference/java/lang/Object#finalize()() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. | | ` final https://developer.android.com/reference/java/lang/Class<?>` | ` https://developer.android.com/reference/java/lang/Object#getClass()() ` Returns the runtime class of this `Object`. | | ` int` | ` https://developer.android.com/reference/java/lang/Object#hashCode()() ` Returns a hash code value for the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notify()() ` Wakes up a single thread that is waiting on this object's monitor. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notifyAll()() ` Wakes up all threads that are waiting on this object's monitor. | | ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/lang/Object#toString()() ` Returns a string representation of the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait()() ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*. | ||

## XML attributes

### android:allowBackup

Whether to allow the application to participate in the backup
and restore infrastructure. If this attribute is set to `false`,
no backup or restore of the application will ever be performed, even by a
full-system backup that would otherwise cause all application data to be saved
via adb. The default value of this attribute is `true`.

May be a boolean value, such as "`true`" or
"`false`".

<br />

### android:backupAgent

The name of the class subclassing `BackupAgent` to manage
backup and restore of the application's data on external storage.

May be a string value, using '\\\\;' to escape characters such as
'\\\\n' or '\\\\uxxxx' for a unicode character;

<br />

### android:killAfterRestore

Whether the application in question should be terminated after its
settings have been restored during a full-system restore operation.
Single-package restore operations will never cause the application to
be shut down. Full-system restore operations typically only occur once,
when the phone is first set up. Third-party applications will not usually
need to use this attribute.

The default is `true`, which means that after the application
has finished processing its data during a full-system restore, it will be
terminated.

May be a boolean value, such as "`true`" or
"`false`".

<br />

### android:restoreAnyVersion

Indicate that the application is prepared to attempt a restore of any
backed-up dataset, even if the backup is apparently from a newer version
of the application than is currently installed on the device. Setting
this attribute to `true` will permit the Backup Manager to
attempt restore even when a version mismatch suggests that the data are
incompatible. *Use with caution!*

The default value of this attribute is `false`.

May be a boolean value, such as "`true`" or
"`false`".

<br />

## Public constructors

### BackupManager

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public BackupManager (Context context)
```

Constructs a BackupManager object through which the application can
communicate with the Android backup system.

<br />

| Parameters ||
|---|---|
| `context` | `Context`: The `https://developer.android.com/reference/android/content/Context` that was provided when one of your application's `https://developer.android.com/reference/android/app/Activity` was created. <br /> |

## Public methods

### dataChanged

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void dataChanged ()
```

Notifies the Android backup system that your application wishes to back up
new changes to its data. A backup operation using your application's
`https://developer.android.com/reference/android/app/backup/BackupAgent` subclass will be scheduled when you
call this method.


Note: This only works if your application is performing Key/Value backups.

<br />

### dataChanged

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static void dataChanged (String packageName)
```

Convenience method for callers who need to indicate that some other package
needs a backup pass. This can be useful in the case of groups of packages
that share a uid.


This method requires that the application hold the "android.permission.BACKUP"
permission if the package named in the argument does not run under the same uid
as the caller.

<br />

| Parameters ||
|---|---|
| `packageName` | `String`: The package name identifying the application to back up. Note: Only works for packages performing Key/Value backups. |

### getUserForAncestralSerialNumber

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public UserHandle getUserForAncestralSerialNumber (long ancestralSerialNumber)
```

Returns a `https://developer.android.com/reference/android/os/UserHandle` for the user that has `ancestralSerialNumber` as the
serial number of the its ancestral work profile or `null` if there is none.

The ancestral serial number will have a corresponding `https://developer.android.com/reference/android/os/UserHandle` if the device
has a work profile that was restored from another work profile with serial number
`ancestralSerialNumber`.

<br />

| Parameters ||
|---|---|
| `ancestralSerialNumber` | `long` <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/os/UserHandle` | <br /> |

**See also:**

- `https://developer.android.com/reference/android/os/UserManager#getSerialNumberForUser(android.os.UserHandle)`

### requestRestore

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)   
Deprecated in [API level
28](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int requestRestore (RestoreObserver observer)
```


**This method was deprecated
in API level 28.**   

Applications shouldn't request a restore operation using this method. In Android
P and later, this method is a no-op.

Restore the calling application from backup. The data will be restored from the
current backup dataset if the application has stored data there, or from
the dataset used during the last full device setup operation if the current
backup dataset has no matching data. If no backup data exists for this application
in either source, a non-zero value is returned.

If this method returns zero (meaning success), the OS attempts to retrieve a backed-up
dataset from the remote transport, instantiate the application's backup agent, and pass the
dataset to the agent's
`https://developer.android.com/reference/android/app/backup/BackupAgent#onRestore(android.app.backup.BackupDataInput,%20int,%20android.os.ParcelFileDescriptor)`
method.

Unlike other restore operations, this method doesn't terminate the
application after the restore. The application continues running to receive the
`https://developer.android.com/reference/android/app/backup/RestoreObserver` callbacks on the `observer` argument. Full backups use an
`https://developer.android.com/reference/android/app/Application` base class while key-value backups use the
application subclass declared in the AndroidManifest.xml `<application>` tag.

<br />

<br />

| Parameters ||
|---|---|
| `observer` | `RestoreObserver`: The `https://developer.android.com/reference/android/app/backup/RestoreObserver` to receive callbacks during the restore operation. This must not be null. <br /> |

| Returns ||
|---|---|
| `int` | Zero on success; nonzero on error. <br /> |