1# UIAbility Backup and Restore
2
3## When to Use
4
5When an application runs in the background, factors such as system resource control may cause the application to close or its process to terminate, which might result in the loss of user data. However, if the application has enabled the [UIAbility](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md) backup and restore feature within [UIAbilityContext](../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md) and saved temporary data, it can restore the previous state and data (including the page stack and the data stored in the [onSaveState](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityonsavestate) callback) when it restarts after being closed, maintaining a seamless user experience.
6
7> **NOTE**
8>
9> If the application is stopped normally, the UIAbility backup process is not triggered. If the application is started normally (for example, by calling the **startAbility** API or clicking the icon), the UIAbility restore process is not triggered.
10
11## Working Mechanism
12- UIAbility data backup: After an application transitions to the [onBackground](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityonbackground) lifecycle, the system automatically calls [onSaveState](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityonsavestate) to back up data.
13- UIAbility data restore: The restored [Want](../reference/apis-ability-kit/js-apis-app-ability-want.md) data can be obtained from the [onCreate](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityoncreate) lifecycle of the application, and the page stack data can be restored in the [onWindowStageCreate](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityonwindowstagecreate) lifecycle of the application.
14
15## Constraints
16
17- The UIAbility backup and restore feature supports multiple instances. Backup data is stored in the application sandbox path as files for seven days.
18
19- Backup data is stored in the form of [WantParams](../reference/apis-ability-kit/js-apis-app-ability-want.md). Due to serialization constraints, the maximum data volume supported is 200 KB.
20
21- Data restore is unavailable after device restart.
22
23- The data backup and restore feature is unavailable for a [UIExtensionAbility](../reference/apis-ability-kit/js-apis-app-ability-uiExtensionAbility.md).
24
25## Available APIs
26
27The UIAbility backup and restore API is provided by the [UIAbilityContext](../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md) module. You can directly use **this.context** in the [UIAbility](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md) to call them. For details, see [How to Develop](#how-to-develop).
28
29| API                                                      | Description                                                |
30| ------------------------------------------------------------ | ---------------------------------------------------- |
31| setRestoreEnabled(enabled: boolean): void | Sets whether to enable restore when the UIAbility is switched back from the background.|
32
33[setRestoreEnabled](../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextsetrestoreenabled14) must be called during application initialization (before [onForeground](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityonforeground) is invoked). For example, it can be called in the [onCreate](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityoncreate) callback of the UIAbility.
34
35
36## How to Develop
37
38To enable UIAbility backup and restore during application module initialization, refer to the code snippet below.
39
40```ts
41import { UIAbility } from '@kit.AbilityKit';
42
43export default class EntryAbility extends UIAbility {
44    onCreate() {
45        console.info("[Demo] EntryAbility onCreate");
46        this.context.setRestoreEnabled(true);
47    }
48}
49```
50
51To proactively save data and restore the data when the UIAbility is started, refer to the code snippet below.
52
53```ts
54import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
55
56export default class EntryAbility extends UIAbility {
57    onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
58        console.info("[Demo] EntryAbility onCreate");
59        this.context.setRestoreEnabled(true);
60        if (want && want.parameters) {
61          let recoveryMyData = want.parameters["myData"];
62        }
63    }
64
65    onSaveState(state:AbilityConstant.StateType, wantParams: Record<string, Object>) {
66        // Ability has called to save app data
67        console.log("[Demo] EntryAbility onSaveState");
68        wantParams["myData"] = "my1234567";
69        return AbilityConstant.OnSaveResult.ALL_AGREE;
70    }
71}
72```
73