1# @ohos.app.ability.appRecovery (appRecovery) 2 3The **appRecovery** module provides APIs for recovering faulty applications. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version. In the current version, only applications with a single ability in a single process can be recovered. 8 9## Modules to Import 10```ts 11import appRecovery from '@ohos.app.ability.appRecovery'; 12``` 13 14 15## appRecovery.RestartFlag 16 17Enumerates the application restart flags. This enum is used as an input parameter of [enableAppRecovery](#apprecoveryenableapprecovery). 18 19**System capability**: SystemCapability.Ability.AbilityRuntime.Core 20 21| Name | Value | Description | 22| ---------- | ---- | ---------- | 23| ALWAYS_RESTART | 0 | The application is restarted in all cases.| 24| RESTART_WHEN_JS_CRASH | 0x0001 | The application is restarted in the case of JS_CRASH. | 25| RESTART_WHEN_APP_FREEZE | 0x0002 | The application is restarted in the case of APP_FREEZE. | 26| NO_RESTART | 0xFFFF | The application is not restarted in any case.| 27 28## appRecovery.SaveOccasionFlag 29 30Enumerates the scenarios for saving the application state. This enum is used as an input parameter of [enableAppRecovery](#apprecoveryenableapprecovery). 31 32**System capability**: SystemCapability.Ability.AbilityRuntime.Core 33 34| Name | Value | Description | 35| ----------------------------- | ---- | ------------------------------------------------------------ | 36| SAVE_WHEN_ERROR | 0x0001 | Saving the application state when an application fault occurs.| 37| SAVE_WHEN_BACKGROUND | 0x0002 | Saving the application state when the application is switched to the background.| 38 39## appRecovery.SaveModeFlag 40 41Enumerates the application state saving modes. This enum is used as an input parameter of [enableAppRecovery](#apprecoveryenableapprecovery). 42 43**System capability**: SystemCapability.Ability.AbilityRuntime.Core 44 45| Name | Value | Description | 46| ----------------------------- | ---- | ------------------------------------------------------------ | 47| SAVE_WITH_FILE | 0x0001 | The application state is saved and written to the local file cache.| 48| SAVE_WITH_SHARED_MEMORY | 0x0002 | The application state is saved in the memory. When the application exits due to a fault, it is written to the local file cache.| 49 50## appRecovery.enableAppRecovery 51 52enableAppRecovery(restart?: [RestartFlag](#apprecoveryrestartflag), saveOccasion?: [SaveOccasionFlag](#apprecoverysaveoccasionflag), saveMode?: [SaveModeFlag](#apprecoverysavemodeflag)) : void; 53 54Enables application recovery. 55 56**System capability**: SystemCapability.Ability.AbilityRuntime.Core 57 58**Parameters** 59 60| Name| Type| Mandatory| Description| 61| -------- | -------- | -------- | -------- | 62| restart | [RestartFlag](#apprecoveryrestartflag) | No| Whether the application is restarted upon a fault. By default, the application is not restarted.| 63| saveOccasion | [SaveOccasionFlag](#apprecoverysaveoccasionflag) | No| Scenario for saving the application state. By default, the state is saved when a fault occurs.| 64| saveMode | [SaveModeFlag](#apprecoverysavemodeflag) | No| Application state saving mode. By default, the application state is written to the local file cache.| 65 66**Example** 67 68```ts 69import appRecovery from '@ohos.app.ability.appRecovery'; 70import AbilityStage from '@ohos.app.ability.AbilityStage'; 71 72export default class MyAbilityStage extends AbilityStage { 73 onCreate() { 74 appRecovery.enableAppRecovery( 75 appRecovery.RestartFlag.ALWAYS_RESTART, 76 appRecovery.SaveOccasionFlag.SAVE_WHEN_ERROR, 77 appRecovery.SaveModeFlag.SAVE_WITH_FILE 78 ); 79 } 80} 81``` 82 83## appRecovery.restartApp 84 85restartApp(): void; 86 87Restarts the application. This API can be used together with APIs of [errorManager](js-apis-app-ability-errorManager.md). 88 89**System capability**: SystemCapability.Ability.AbilityRuntime.Core 90 91 92**Example** 93 94```ts 95import appRecovery from '@ohos.app.ability.appRecovery'; 96import errorManager from '@ohos.app.ability.errorManager'; 97 98let observer = { 99 onUnhandledException(errorMsg) { 100 console.log('onUnhandledException, errorMsg: ', errorMsg); 101 appRecovery.restartApp(); 102 } 103}; 104 105try { 106 errorManager.on('error', observer); 107} catch (paramError) { 108 console.log('error: ' + paramError.code + ', ' + paramError.message); 109} 110``` 111 112## appRecovery.saveAppState 113 114saveAppState(): boolean; 115 116Saves the application state. This API can be used together with APIs of [errorManager](js-apis-app-ability-errorManager.md). 117 118**System capability**: SystemCapability.Ability.AbilityRuntime.Core 119 120**Return value** 121 122| Type| Description| 123| -------- | -------- | 124| boolean | Whether the application state is saved. The value **true** is returned if the application state is saved, and **false** is returned otherwise.| 125 126**Example** 127 128```ts 129import appRecovery from '@ohos.app.ability.appRecovery'; 130import errorManager from '@ohos.app.ability.errorManager'; 131 132let observer = { 133 onUnhandledException(errorMsg) { 134 console.log('onUnhandledException, errorMsg: ', errorMsg); 135 appRecovery.saveAppState(); 136 } 137}; 138 139try { 140 errorManager.on('error', observer); 141} catch (paramError) { 142 console.log('error: ' + paramError.code + ', ' + paramError.message); 143} 144``` 145