• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Interface (WindowStage)
2<!--Kit: ArkUI-->
3<!--Subsystem: Window-->
4<!--Owner: @waterwin-->
5<!--Designer: @nyankomiya-->
6<!--Tester: @qinliwen0417-->
7<!--Adviser: @ge-yafang-->
8
9> **NOTE**
10>
11> - The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
12>
13> - The initial APIs of this interface are supported since API version 9.
14
15Implements a window manager, which manages each basic window unit, that is, [Window](arkts-apis-window-Window.md) instance.
16
17Before calling any of the following APIs, you must use [onWindowStageCreate()](../apis-ability-kit/js-apis-app-ability-uiAbility.md#onwindowstagecreate) to create a WindowStage instance.
18
19## Modules to Import
20
21```ts
22import { window } from '@kit.ArkUI';
23```
24
25## getMainWindow<sup>9+</sup>
26
27getMainWindow(callback: AsyncCallback&lt;Window&gt;): void
28
29Obtains the main window of this WindowStage. This API uses an asynchronous callback to return the result.
30
31**Model restriction**: This API can be used only in the stage model.
32
33**System capability**: SystemCapability.WindowManager.WindowManager.Core
34
35**Atomic service API**: This API can be used in atomic services since API version 11.
36
37**Parameters**
38
39| Name  | Type                                  | Mandatory| Description                                         |
40| -------- | -------------------------------------- | ---- | --------------------------------------------- |
41| callback | AsyncCallback&lt;[Window](arkts-apis-window-Window.md)&gt; | Yes  | Callback used to return the main window.|
42
43**Error codes**
44
45For details about the error codes, see [Window Error Codes](errorcode-window.md).
46
47| ID| Error Message|
48| ------- | ------------------------------ |
49| 1300002 | This window state is abnormal. |
50| 1300005 | This window stage is abnormal. |
51
52**Example**
53
54```ts
55// EntryAbility.ets
56import { UIAbility } from '@kit.AbilityKit';
57import { BusinessError } from '@kit.BasicServicesKit';
58
59export default class EntryAbility extends UIAbility {
60  // ...
61
62  onWindowStageCreate(windowStage: window.WindowStage) {
63    console.info('onWindowStageCreate');
64    let windowClass: window.Window | undefined = undefined;
65    windowStage.getMainWindow((err: BusinessError, data) => {
66      const errCode: number = err.code;
67      if (errCode) {
68        console.error(`Failed to obtain the main window. Cause code: ${err.code}, message: ${err.message}`);
69        return;
70      }
71      windowClass = data;
72      console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data));
73    });
74  }
75};
76```
77
78## getMainWindow<sup>9+</sup>
79
80getMainWindow(): Promise&lt;Window&gt;
81
82Obtains the main window of this WindowStage. This API uses a promise to return the result.
83
84**Model restriction**: This API can be used only in the stage model.
85
86**System capability**: SystemCapability.WindowManager.WindowManager.Core
87
88**Atomic service API**: This API can be used in atomic services since API version 11.
89
90**Return value**
91
92| Type                            | Description                                            |
93| -------------------------------- | ------------------------------------------------ |
94| Promise&lt;[Window](arkts-apis-window-Window.md)&gt; | Promise used to return the main window.|
95
96**Error codes**
97
98For details about the error codes, see [Window Error Codes](errorcode-window.md).
99
100| ID| Error Message|
101| ------- | ------------------------------ |
102| 1300002 | This window state is abnormal. |
103| 1300005 | This window stage is abnormal. |
104
105**Example**
106
107```ts
108// EntryAbility.ets
109import { UIAbility } from '@kit.AbilityKit';
110import { BusinessError } from '@kit.BasicServicesKit';
111
112export default class EntryAbility extends UIAbility {
113  // ...
114
115  onWindowStageCreate(windowStage: window.WindowStage) {
116    console.info('onWindowStageCreate');
117    let windowClass: window.Window | undefined = undefined;
118    let promise = windowStage.getMainWindow();
119    promise.then((data) => {
120      windowClass = data;
121      console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data));
122    }).catch((err: BusinessError) => {
123      console.error(`Failed to obtain the main window. Cause code: ${err.code}, message: ${err.message}`);
124    });
125  }
126};
127```
128
129## getMainWindowSync<sup>9+</sup>
130
131getMainWindowSync(): Window
132
133Obtains the main window of this WindowStage.
134
135**Model restriction**: This API can be used only in the stage model.
136
137**System capability**: SystemCapability.WindowManager.WindowManager.Core
138
139**Atomic service API**: This API can be used in atomic services since API version 11.
140
141**Return value**
142
143| Type| Description|
144| ----------------- | --------------------------------- |
145| [Window](arkts-apis-window-Window.md) | Main window.|
146
147**Error codes**
148
149For details about the error codes, see [Window Error Codes](errorcode-window.md).
150
151| ID| Error Message|
152| ------- | ------------------------------ |
153| 1300002 | This window state is abnormal. |
154| 1300005 | This window stage is abnormal. |
155
156**Example**
157<!--code_no_check-->
158```ts
159// EntryAbility.ets
160import { UIAbility } from '@kit.AbilityKit';
161
162export default class EntryAbility extends UIAbility {
163  // ...
164
165  onWindowStageCreate(windowStage: window.WindowStage) {
166    console.info('onWindowStageCreate');
167    try {
168      let windowClass = windowStage.getMainWindowSync();
169    } catch (exception) {
170      console.error(`Failed to obtain the main window. Cause code: ${exception.code}, message: ${exception.message}`);
171    }
172  }
173};
174```
175
176## createSubWindow<sup>9+</sup>
177
178createSubWindow(name: string, callback: AsyncCallback&lt;Window&gt;): void
179
180Creates a child window for this WindowStage. This API uses an asynchronous callback to return the result.
181
182**Model restriction**: This API can be used only in the stage model.
183
184**System capability**: SystemCapability.WindowManager.WindowManager.Core
185
186**Atomic service API**: This API can be used in atomic services since API version 11.
187
188**Parameters**
189
190| Name  | Type                                  | Mandatory| Description                                         |
191| -------- | -------------------------------------- | ---- | --------------------------------------------- |
192| name     | string                                 | Yes  | Name of the child window.                               |
193| callback | AsyncCallback&lt;[Window](arkts-apis-window-Window.md)&gt; | Yes  | Callback used to return the child window.|
194
195**Error codes**
196
197For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Window Error Codes](errorcode-window.md).
198
199| ID| Error Message|
200| ------- | ------------------------------ |
201| 401     | Parameter error. Possible cause: Incorrect parameter types. |
202| 1300002 | This window state is abnormal. |
203
204**Example**
205
206```ts
207// EntryAbility.ets
208import { UIAbility } from '@kit.AbilityKit';
209import { BusinessError } from '@kit.BasicServicesKit';
210
211export default class EntryAbility extends UIAbility {
212  // ...
213
214  onWindowStageCreate(windowStage: window.WindowStage) {
215    console.info('onWindowStageCreate');
216    let windowClass: window.Window | undefined = undefined;
217    try {
218      windowStage.createSubWindow('mySubWindow', (err: BusinessError, data) => {
219        const errCode: number = err.code;
220        if (errCode) {
221          console.error(`Failed to create the subwindow. Cause code: ${err.code}, message: ${err.message}`);
222          return;
223        }
224        windowClass = data;
225        console.info('Succeeded in creating the subwindow. Data: ' + JSON.stringify(data));
226        if (!windowClass) {
227          console.info('Failed to load the content. Cause: windowClass is null');
228        }
229        else {
230          windowClass.resize(500, 1000);
231        }
232      });
233    } catch (exception) {
234      console.error(`Failed to create the subwindow. Cause code: ${exception.code}, message: ${exception.message}`);
235    }
236  }
237};
238```
239
240## createSubWindow<sup>9+</sup>
241
242createSubWindow(name: string): Promise&lt;Window&gt;
243
244Creates a child window for this WindowStage. This API uses a promise to return the result.
245
246**Model restriction**: This API can be used only in the stage model.
247
248**System capability**: SystemCapability.WindowManager.WindowManager.Core
249
250**Atomic service API**: This API can be used in atomic services since API version 11.
251
252**Parameters**
253
254| Name| Type  | Mandatory| Description          |
255| ------ | ------ | ---- | -------------- |
256| name   | string | Yes  | Name of the child window.|
257
258**Return value**
259
260| Type                            | Description                                            |
261| -------------------------------- | ------------------------------------------------ |
262| Promise&lt;[Window](arkts-apis-window-Window.md)&gt; | Promise used to return the child window.|
263
264**Error codes**
265
266For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Window Error Codes](errorcode-window.md).
267
268| ID| Error Message|
269| ------- | ------------------------------ |
270| 401     | Parameter error. Possible cause: Incorrect parameter types. |
271| 1300002 | This window state is abnormal. |
272
273**Example**
274
275```ts
276// EntryAbility.ets
277import { UIAbility } from '@kit.AbilityKit';
278import { BusinessError } from '@kit.BasicServicesKit';
279
280export default class EntryAbility extends UIAbility {
281  // ...
282
283  onWindowStageCreate(windowStage: window.WindowStage) {
284    console.info('onWindowStageCreate');
285    let windowClass: window.Window | undefined = undefined;
286    try {
287      let promise = windowStage.createSubWindow('mySubWindow');
288      promise.then((data) => {
289        windowClass = data;
290        console.info('Succeeded in creating the subwindow. Data: ' + JSON.stringify(data));
291      }).catch((err: BusinessError) => {
292        console.error(`Failed to create the subwindow. Cause code: ${err.code}, message: ${err.message}`);
293      });
294    } catch (exception) {
295      console.error(`Failed to create the subwindow. Cause code: ${exception.code}, message: ${exception.message}`);
296    }
297  }
298};
299```
300
301## createSubWindowWithOptions<sup>11+</sup>
302
303createSubWindowWithOptions(name: string, options: SubWindowOptions): Promise&lt;Window&gt;
304
305Creates a child window for this WindowStage. This API uses a promise to return the result.
306
307**Model restriction**: This API can be used only in the stage model.
308
309**Atomic service API**: This API can be used in atomic services since API version 12.
310
311**System capability**: SystemCapability.Window.SessionManager
312
313**Parameters**
314
315| Name| Type  | Mandatory| Description          |
316| ------ | ------ | ---- | -------------- |
317| name   | string | Yes  | Name of the child window.|
318| options  | [SubWindowOptions](arkts-apis-window-i.md#subwindowoptions11) | Yes  | Parameters used for creating the child window. |
319
320**Return value**
321
322| Type                            | Description                                            |
323| -------------------------------- | ------------------------------------------------ |
324| Promise&lt;[Window](arkts-apis-window-Window.md)&gt; | Promise used to return the child window created.|
325
326**Error codes**
327
328For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Window Error Codes](errorcode-window.md).
329
330| ID| Error Message|
331| ------- | ------------------------------ |
332| 401     | Parameter error. Possible cause: Incorrect parameter types. |
333| 801     | Capability not supported. Failed to call the API due to limited device capabilities. |
334| 1300002 | This window state is abnormal. |
335| 1300005 | This window stage is abnormal. |
336
337**Example**
338
339```ts
340// EntryAbility.ets
341import { UIAbility } from '@kit.AbilityKit';
342import { BusinessError } from '@kit.BasicServicesKit';
343
344export default class EntryAbility extends UIAbility {
345  // ...
346
347  onWindowStageCreate(windowStage: window.WindowStage) {
348    console.info('onWindowStageCreate');
349    let windowClass: window.Window | undefined = undefined;
350    try {
351      let options : window.SubWindowOptions = {
352        title: 'title',
353        decorEnabled: true
354      };
355      let promise = windowStage.createSubWindowWithOptions('mySubWindow', options);
356      promise.then((data) => {
357        windowClass = data;
358        console.info('Succeeded in creating the subwindow. Data: ' + JSON.stringify(data));
359      }).catch((err: BusinessError) => {
360        console.error(`Failed to create the subwindow. Cause code: ${err.code}, message: ${err.message}`);
361      });
362    } catch (exception) {
363      console.error(`Failed to create the subwindow. Cause code: ${exception.code}, message: ${exception.message}`);
364    }
365  }
366};
367```
368
369## getSubWindow<sup>9+</sup>
370
371getSubWindow(callback: AsyncCallback&lt;Array&lt;Window&gt;&gt;): void
372
373Obtains all the child windows of this WindowStage. This API uses an asynchronous callback to return the result.
374
375**Model restriction**: This API can be used only in the stage model.
376
377**System capability**: SystemCapability.WindowManager.WindowManager.Core
378
379**Atomic service API**: This API can be used in atomic services since API version 11.
380
381**Parameters**
382
383| Name  | Type                                               | Mandatory| Description                                             |
384| -------- | --------------------------------------------------- | ---- | ------------------------------------------------- |
385| callback | AsyncCallback&lt;Array&lt;[Window](arkts-apis-window-Window.md)&gt;&gt; | Yes  | Callback used to return all the child windows.|
386
387**Error codes**
388
389For details about the error codes, see [Window Error Codes](errorcode-window.md).
390
391| ID| Error Message|
392| ------- | ------------------------------ |
393| 1300002 | This window state is abnormal. |
394
395**Example**
396<!--code_no_check-->
397```ts
398// EntryAbility.ets
399import { UIAbility } from '@kit.AbilityKit';
400import { BusinessError } from '@kit.BasicServicesKit';
401
402export default class EntryAbility extends UIAbility {
403  // ...
404
405  onWindowStageCreate(windowStage: window.WindowStage) {
406    console.info('onWindowStageCreate');
407    let windowClass: window.Window[] = [];
408    windowStage.getSubWindow((err: BusinessError, data) => {
409      const errCode: number = err.code;
410      if (errCode) {
411        console.error(`Failed to obtain the subwindow. Cause code: ${err.code}, message: ${err.message}`);
412        return;
413      }
414      windowClass = data;
415      console.info('Succeeded in obtaining the subwindow. Data: ' + JSON.stringify(data));
416    });
417  }
418};
419```
420
421## getSubWindow<sup>9+</sup>
422
423getSubWindow(): Promise&lt;Array&lt;Window&gt;&gt;
424
425Obtains all the child windows of this WindowStage. This API uses a promise to return the result.
426
427**Model restriction**: This API can be used only in the stage model.
428
429**System capability**: SystemCapability.WindowManager.WindowManager.Core
430
431**Atomic service API**: This API can be used in atomic services since API version 11.
432
433**Return value**
434
435| Type                                         | Description                                                |
436| --------------------------------------------- | ---------------------------------------------------- |
437| Promise&lt;Array&lt;[Window](arkts-apis-window-Window.md)&gt;&gt; | Promise used to return all the child windows.|
438
439**Error codes**
440
441For details about the error codes, see [Window Error Codes](errorcode-window.md).
442
443| ID| Error Message|
444| ------- | ------------------------------ |
445| 1300002 | This window state is abnormal. |
446
447**Example**
448<!--code_no_check-->
449```ts
450// EntryAbility.ets
451import { UIAbility } from '@kit.AbilityKit';
452import { BusinessError } from '@kit.BasicServicesKit';
453
454export default class EntryAbility extends UIAbility {
455  // ...
456
457  onWindowStageCreate(windowStage: window.WindowStage) {
458    console.info('onWindowStageCreate');
459    let windowClass: window.Window[] = [];
460    let promise = windowStage.getSubWindow();
461    promise.then((data) => {
462      windowClass = data;
463      console.info('Succeeded in obtaining the subwindow. Data: ' + JSON.stringify(data));
464    }).catch((err: BusinessError) => {
465      console.error(`Failed to obtain the subwindow. Cause code: ${err.code}, message: ${err.message}`);
466    });
467  }
468};
469```
470
471## loadContent<sup>9+</sup>
472
473loadContent(path: string, storage: LocalStorage, callback: AsyncCallback&lt;void&gt;): void
474
475Loads the content of a page, with its path in the current project specified, to the main window of this WindowStage, and transfers the state attribute to the page through a local storage. This API uses an asynchronous callback to return the result. You are advised to call this API during UIAbility startup. If called repeatedly, this API will destroy the existing page content (UIContent) before loading the new content. Exercise caution when using it. The execution context of the current UI may be unclear. Therefore, you are advised not to perform UI-related operations within the callback.
476
477**Model restriction**: This API can be used only in the stage model.
478
479**System capability**: SystemCapability.WindowManager.WindowManager.Core
480
481**Atomic service API**: This API can be used in atomic services since API version 11.
482
483**Parameters**
484
485| Name  | Type                                           | Mandatory| Description                                                        |
486| -------- | ----------------------------------------------- | ---- | ------------------------------------------------------------ |
487| path     | string                                          | Yes  | Path of the page from which the content will be loaded. The path is configured in the **main_pages.json** file of the project. |
488| storage  | [LocalStorage](../../ui/state-management/arkts-localstorage.md) | Yes  | Page-level UI state storage unit, which is used to transfer the state attribute for the page.|
489| callback | AsyncCallback&lt;void&gt;                       | Yes  | Callback used to return the result.                                                  |
490
491**Error codes**
492
493For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Window Error Codes](errorcode-window.md).
494
495| ID| Error Message|
496| ------- | ------------------------------ |
497| 401     | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Invalid path parameter.|
498| 1300002 | This window state is abnormal. |
499
500**Example**
501
502```ts
503// EntryAbility.ets
504import { UIAbility } from '@kit.AbilityKit';
505import { BusinessError } from '@kit.BasicServicesKit';
506
507export default class EntryAbility extends UIAbility {
508  // ...
509
510  storage: LocalStorage = new LocalStorage();
511
512  onWindowStageCreate(windowStage: window.WindowStage) {
513    this.storage.setOrCreate('storageSimpleProp', 121);
514    console.info('onWindowStageCreate');
515    try {
516      windowStage.loadContent('pages/page2', this.storage, (err: BusinessError) => {
517        const errCode: number = err.code;
518        if (errCode) {
519          console.error(`Failed to load the content. Cause code: ${err.code}, message: ${err.message}`);
520          return;
521        }
522        console.info('Succeeded in loading the content.');
523      });
524    } catch (exception) {
525      console.error(`Failed to load the content. Cause code: ${exception.code}, message: ${exception.message}`);
526    }
527  }
528};
529```
530
531## loadContent<sup>9+</sup>
532
533loadContent(path: string, storage?: LocalStorage): Promise&lt;void&gt;
534
535Loads the content of a page, with its path in the current project specified, to the main window of this WindowStage, and transfers the state attribute to the page through a local storage. This API uses a promise to return the result. You are advised to call this API during UIAbility startup. If called repeatedly, this API will destroy the existing page content (UIContent) before loading the new content. Exercise caution when using it. The execution context of the current UI may be unclear. Therefore, you are advised not to perform UI-related operations within the callback.
536
537**Model restriction**: This API can be used only in the stage model.
538
539**System capability**: SystemCapability.WindowManager.WindowManager.Core
540
541**Atomic service API**: This API can be used in atomic services since API version 11.
542
543**Parameters**
544
545| Name | Type                                           | Mandatory| Description                                                        |
546| ------- | ----------------------------------------------- | ---- | ------------------------------------------------------------ |
547| path    | string                                          | Yes  | Path of the page from which the content will be loaded. The path is configured in the **main_pages.json** file of the project.|
548| storage | [LocalStorage](../../ui/state-management/arkts-localstorage.md) | No  | Page-level UI state storage unit, which is used to transfer the state attribute for the page.|
549
550**Return value**
551
552| Type               | Description                     |
553| ------------------- | ------------------------- |
554| Promise&lt;void&gt; | Promise that returns no value.|
555
556**Error codes**
557
558For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Window Error Codes](errorcode-window.md).
559
560| ID| Error Message|
561| ------- | ------------------------------ |
562| 401     | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Invalid path parameter.|
563| 1300002 | This window state is abnormal. |
564
565**Example**
566
567```ts
568// EntryAbility.ets
569import { UIAbility } from '@kit.AbilityKit';
570import { BusinessError } from '@kit.BasicServicesKit';
571
572export default class EntryAbility extends UIAbility {
573  // ...
574
575  storage: LocalStorage = new LocalStorage();
576
577  onWindowStageCreate(windowStage: window.WindowStage) {
578    this.storage.setOrCreate('storageSimpleProp', 121);
579    console.info('onWindowStageCreate');
580    try {
581      let promise = windowStage.loadContent('pages/page2', this.storage);
582      promise.then(() => {
583        console.info('Succeeded in loading the content.');
584      }).catch((err: BusinessError) => {
585        console.error(`Failed to load the content. Cause code: ${err.code}, message: ${err.message}`);
586      });
587    } catch (exception) {
588      console.error(`Failed to load the content. Cause code: ${exception.code}, message: ${exception.message}`);
589    }
590    ;
591  }
592};
593```
594
595## loadContent<sup>9+</sup>
596
597loadContent(path: string, callback: AsyncCallback&lt;void&gt;): void
598
599Loads the content of a page, with its path in the current project specified, to the main window of this WindowStage. This API uses an asynchronous callback to return the result. You are advised to call this API during UIAbility startup. If called repeatedly, this API will destroy the existing page content (UIContent) before loading the new content. Exercise caution when using it. The execution context of the current UI may be unclear. Therefore, you are advised not to perform UI-related operations within the callback.
600
601**Model restriction**: This API can be used only in the stage model.
602
603**System capability**: SystemCapability.WindowManager.WindowManager.Core
604
605**Atomic service API**: This API can be used in atomic services since API version 11.
606
607**Parameters**
608
609| Name  | Type                     | Mandatory| Description                |
610| -------- | ------------------------- | ---- | -------------------- |
611| path     | string                    | Yes  | Path of the page from which the content will be loaded. The path is configured in the **main_pages.json** file of the project.|
612| callback | AsyncCallback&lt;void&gt; | Yes  | Callback used to return the result.          |
613
614**Error codes**
615
616For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Window Error Codes](errorcode-window.md).
617
618| ID| Error Message|
619| ------- | ------------------------------ |
620| 401     | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Invalid path parameter.|
621| 1300002 | This window state is abnormal. |
622
623**Example**
624
625```ts
626// EntryAbility.ets
627import { UIAbility } from '@kit.AbilityKit';
628import { BusinessError } from '@kit.BasicServicesKit';
629
630export default class EntryAbility extends UIAbility {
631  // ...
632
633  onWindowStageCreate(windowStage: window.WindowStage) {
634    console.info('onWindowStageCreate');
635    try {
636      windowStage.loadContent('pages/page2', (err: BusinessError) => {
637        const errCode: number = err.code;
638        if (errCode) {
639          console.error(`Failed to load the content. Cause code: ${err.code}, message: ${err.message}`);
640          return;
641        }
642        console.info('Succeeded in loading the content.');
643      });
644    } catch (exception) {
645      console.error(`Failed to load the content. Cause code: ${exception.code}, message: ${exception.message}`);
646    }
647  }
648};
649```
650
651## loadContentByName<sup>11+</sup>
652
653loadContentByName(name: string, storage: LocalStorage, callback: AsyncCallback&lt;void&gt;): void
654
655Loads the content of a [named route](../../ui/arkts-routing.md#named-route) page to this WindowStage, and transfers the state attribute to the page through a local storage. This API uses an asynchronous callback to return the result. You are advised to call this API during UIAbility startup. If called repeatedly, this API will destroy the existing page content (UIContent) before loading the new content. Exercise caution when using it. The execution context of the current UI may be unclear. Therefore, you are advised not to perform UI-related operations within the callback.
656
657**Model restriction**: This API can be used only in the stage model.
658
659**System capability**: SystemCapability.WindowManager.WindowManager.Core
660
661**Atomic service API**: This API can be used in atomic services since API version 11.
662
663**Parameters**
664
665| Name  | Type                                                   | Mandatory| Description                                                        |
666| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ |
667| name     | string                                                  | Yes  | Name of the named route page.                                            |
668| storage  | [LocalStorage](../../ui/state-management/arkts-localstorage.md) | Yes  | Page-level UI state storage unit, which is used to transfer the state attribute for the page.|
669| callback | AsyncCallback&lt;void&gt;                               | Yes  | Callback used to return the result.                                                  |
670
671**Error codes**
672
673For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Window Error Codes](errorcode-window.md).
674
675| ID| Error Message                                     |
676| -------- | --------------------------------------------- |
677| 401      | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
678| 1300002  | This window state is abnormal.                |
679
680**Example**
681
682<!--code_no_check-->
683```ts
684// EntryAbility.ets
685import { UIAbility } from '@kit.AbilityKit';
686import { BusinessError } from '@kit.BasicServicesKit';
687import * as Index from '../pages/Index'; // Import the named route page.
688
689export default class EntryAbility extends UIAbility {
690  // ...
691
692  storage: LocalStorage = new LocalStorage();
693
694  onWindowStageCreate(windowStage: window.WindowStage) {
695    console.info('onWindowStageCreate');
696    this.storage.setOrCreate('storageSimpleProp', 121);
697    try {
698      windowStage.loadContentByName(Index.entryName, this.storage, (err: BusinessError) => {
699        const errCode: number = err.code;
700        if (errCode) {
701          console.error(`Failed to load the content. Cause code: ${err.code}, message: ${err.message}`);
702          return;
703        }
704        console.info('Succeeded in loading the content.');
705      });
706    } catch (exception) {
707      console.error(`Failed to load the content. Cause code: ${exception.code}, message: ${exception.message}`);
708    }
709  }
710};
711```
712<!--code_no_check-->
713```ts
714// ets/pages/Index.ets
715export const entryName : string = 'Index';
716@Entry({routeName: entryName, useSharedStorage: true})
717@Component
718export struct Index {
719  @State message: string = 'Hello World'
720  @LocalStorageLink('storageSimpleProp') storageSimpleProp: number = 1;
721  build() {
722    Row() {
723      Column() {
724        Text(this.message)
725          .fontSize(50)
726          .fontWeight(FontWeight.Bold)
727      }
728      .width('100%')
729    }
730    .height('100%')
731  }
732}
733```
734
735## loadContentByName<sup>11+</sup>
736
737loadContentByName(name: string, callback: AsyncCallback&lt;void&gt;): void
738
739Loads the content of a [named route](../../ui/arkts-routing.md#named-route) page to this WindowStage. This API uses an asynchronous callback to return the result. You are advised to call this API during UIAbility startup. If called repeatedly, this API will destroy the existing page content (UIContent) before loading the new content. Exercise caution when using it. The execution context of the current UI may be unclear. Therefore, you are advised not to perform UI-related operations within the callback.
740
741**Model restriction**: This API can be used only in the stage model.
742
743**System capability**: SystemCapability.WindowManager.WindowManager.Core
744
745**Atomic service API**: This API can be used in atomic services since API version 11.
746
747**Parameters**
748
749| Name  | Type                     | Mandatory| Description            |
750| -------- | ------------------------- | ---- | ---------------- |
751| name     | string                    | Yes  | Name of the named route page.|
752| callback | AsyncCallback&lt;void&gt; | Yes  | Callback used to return the result.      |
753
754**Error codes**
755
756For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Window Error Codes](errorcode-window.md).
757
758| ID| Error Message                                     |
759| -------- | --------------------------------------------- |
760| 401      | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
761| 1300002  | This window state is abnormal.                |
762
763**Example**
764
765<!--code_no_check-->
766```ts
767// EntryAbility.ets
768import { UIAbility } from '@kit.AbilityKit';
769import { BusinessError } from '@kit.BasicServicesKit';
770import * as Index from '../pages/Index'; // Import the named route page.
771
772export default class EntryAbility extends UIAbility {
773  // ...
774
775  onWindowStageCreate(windowStage: window.WindowStage) {
776    console.info('onWindowStageCreate');
777    try {
778      windowStage.loadContentByName(Index.entryName, (err: BusinessError) => {
779        const errCode: number = err.code;
780        if (errCode) {
781          console.error(`Failed to load the content. Cause code: ${err.code}, message: ${err.message}`);
782          return;
783        }
784        console.info('Succeeded in loading the content.');
785      });
786    } catch (exception) {
787      console.error(`Failed to load the content. Cause code: ${exception.code}, message: ${exception.message}`);
788    }
789  }
790};
791```
792<!--code_no_check-->
793```ts
794// ets/pages/Index.ets
795export const entryName : string = 'Index';
796@Entry({routeName: entryName})
797@Component
798export struct Index {
799  @State message: string = 'Hello World'
800  build() {
801    Row() {
802      Column() {
803        Text(this.message)
804          .fontSize(50)
805          .fontWeight(FontWeight.Bold)
806      }
807      .width('100%')
808    }
809    .height('100%')
810  }
811}
812```
813
814## loadContentByName<sup>11+</sup>
815
816loadContentByName(name: string, storage?: LocalStorage): Promise&lt;void&gt;
817
818Loads the content of a [named route](../../ui/arkts-routing.md#named-route) page to this WindowStage, and transfers the state attribute to the page through a local storage. This API uses a promise to return the result. You are advised to call this API during UIAbility startup. If called repeatedly, this API will destroy the existing page content (UIContent) before loading the new content. Exercise caution when using it. The execution context of the current UI may be unclear. Therefore, you are advised not to perform UI-related operations within the callback.
819
820**Model restriction**: This API can be used only in the stage model.
821
822**System capability**: SystemCapability.WindowManager.WindowManager.Core
823
824**Atomic service API**: This API can be used in atomic services since API version 11.
825
826**Parameters**
827
828| Name | Type        | Mandatory| Description                                                        |
829| ------- | ------------ | ---- | ------------------------------------------------------------ |
830| name    | string       | Yes  | Name of the named route page.                                            |
831| storage | [LocalStorage](../../ui/state-management/arkts-localstorage.md) | No  | Page-level UI state storage unit, which is used to transfer the state attribute for the page.|
832
833**Return value**
834
835| Type               | Description                     |
836| ------------------- | ------------------------- |
837| Promise&lt;void&gt; | Promise that returns no value.|
838
839**Error codes**
840
841For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Window Error Codes](errorcode-window.md).
842
843| ID| Error Message                                     |
844| -------- | --------------------------------------------- |
845| 401      | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
846| 1300002  | This window state is abnormal.                |
847
848**Example**
849
850<!--code_no_check-->
851```ts
852// EntryAbility.ets
853import { UIAbility } from '@kit.AbilityKit';
854import { BusinessError } from '@kit.BasicServicesKit';
855import * as Index from '../pages/Index'; // Import the named route page.
856
857export default class EntryAbility extends UIAbility {
858  // ...
859
860  storage: LocalStorage = new LocalStorage();
861
862  onWindowStageCreate(windowStage: window.WindowStage) {
863    console.info('onWindowStageCreate');
864    this.storage.setOrCreate('storageSimpleProp', 121);
865    try {
866      let promise = windowStage.loadContentByName(Index.entryName, this.storage);
867      promise.then(() => {
868        console.info('Succeeded in loading the content.');
869      }).catch((err: BusinessError) => {
870        console.error(`Failed to load the content. Cause code: ${err.code}, message: ${err.message}`);
871      });
872    } catch (exception) {
873      console.error(`Failed to load the content. Cause code: ${exception.code}, message: ${exception.message}`);
874    }
875  }
876};
877```
878<!--code_no_check-->
879```ts
880// ets/pages/Index.ets
881export const entryName : string = 'Index';
882@Entry({routeName: entryName, useSharedStorage: true})
883@Component
884export struct Index {
885  @State message: string = 'Hello World'
886  @LocalStorageLink('storageSimpleProp') storageSimpleProp: number = 1;
887  build() {
888    Row() {
889      Column() {
890        Text(this.message)
891          .fontSize(50)
892          .fontWeight(FontWeight.Bold)
893      }
894      .width('100%')
895    }
896    .height('100%')
897  }
898}
899```
900
901## on('windowStageEvent')<sup>9+</sup>
902
903on(eventType: 'windowStageEvent', callback: Callback&lt;WindowStageEventType&gt;): void
904
905Subscribes to the WindowStage lifecycle change event.
906
907**Model restriction**: This API can be used only in the stage model.
908
909**System capability**: SystemCapability.WindowManager.WindowManager.Core
910
911**Atomic service API**: This API can be used in atomic services since API version 11.
912
913**Parameters**
914
915| Name  | Type                                                        | Mandatory| Description                                                        |
916| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
917| eventType  | string                                                       | Yes  | Event type. The value is fixed at **'windowStageEvent'**, indicating the WindowStage lifecycle change event.|
918| callback | Callback&lt;[WindowStageEventType](arkts-apis-window-e.md#windowstageeventtype9)&gt; | Yes  | Callback used to return the WindowStage lifecycle state.               |
919
920**Error codes**
921
922For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Window Error Codes](errorcode-window.md).
923
924| ID| Error Message|
925| ------- | ------------------------------ |
926| 401     | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
927| 1300002 | This window state is abnormal. |
928| 1300005 | This window stage is abnormal. |
929
930**Example**
931
932```ts
933// EntryAbility.ets
934import { UIAbility } from '@kit.AbilityKit';
935
936export default class EntryAbility extends UIAbility {
937  // ...
938
939  onWindowStageCreate(windowStage: window.WindowStage) {
940    console.info('onWindowStageCreate');
941    try {
942      windowStage.on('windowStageEvent', (data) => {
943        console.info('Succeeded in enabling the listener for window stage event changes. Data: ' +
944        JSON.stringify(data));
945      });
946    } catch (exception) {
947      console.error(`Failed to enable the listener for window stage event changes. Cause code: ${exception.code}, message: ${exception.message}`);
948    }
949  }
950};
951```
952
953## off('windowStageEvent')<sup>9+</sup>
954
955off(eventType: 'windowStageEvent', callback?: Callback&lt;WindowStageEventType&gt;): void
956
957Unsubscribes from the WindowStage lifecycle change event.
958
959**Model restriction**: This API can be used only in the stage model.
960
961**System capability**: SystemCapability.WindowManager.WindowManager.Core
962
963**Atomic service API**: This API can be used in atomic services since API version 11.
964
965**Parameters**
966
967| Name  | Type                                                        | Mandatory| Description                                                        |
968| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
969| eventType  | string                                                       | Yes  | Event type. The value is fixed at **'windowStageEvent'**, indicating the WindowStage lifecycle change event.|
970| callback | Callback&lt;[WindowStageEventType](arkts-apis-window-e.md#windowstageeventtype9)&gt; | No  | Callback used to return the WindowStage lifecycle state. If a value is passed in, the corresponding subscription is canceled. If no value is passed in, all subscriptions to the specified event are canceled.               |
971
972**Error codes**
973
974For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Window Error Codes](errorcode-window.md).
975
976| ID| Error Message|
977| ------- | ------------------------------ |
978| 401     | Parameter error. Possible cause: 1. Incorrect parameter types; 2. Parameter verification failed. |
979| 1300002 | This window state is abnormal. |
980| 1300005 | This window stage is abnormal. |
981
982**Example**
983
984```ts
985// EntryAbility.ets
986import { UIAbility } from '@kit.AbilityKit';
987
988export default class EntryAbility extends UIAbility {
989  // ...
990
991  onWindowStageCreate(windowStage: window.WindowStage) {
992    console.info('onWindowStageCreate');
993    const callback = (windowStageEventType: window.WindowStageEventType) => {
994      // ...
995    }
996    try {
997      windowStage.on('windowStageEvent', callback);
998    } catch (exception) {
999      console.error(`Failed to enable the listener for window stage event changes. Cause code: ${exception.code}, message: ${exception.message}`);
1000    }
1001    try {
1002      windowStage.off('windowStageEvent', callback);
1003      // Unregister all the callbacks that have been registered through on().
1004      windowStage.off('windowStageEvent');
1005    } catch (exception) {
1006      console.error(`Failed to disable the listener for window stage event changes. Cause code: ${exception.code}, message: ${exception.message}`);
1007    }
1008  }
1009};
1010```
1011
1012## on('windowStageLifecycleEvent')<sup>20+</sup>
1013
1014on(eventType: 'windowStageLifecycleEvent', callback: Callback&lt;WindowStageLifecycleEventType&gt;): void
1015
1016Subscribes to the WindowStage lifecycle change event.
1017
1018> **NOTE**
1019>
1020>  Comparison with [on('windowStageEvent')](#onwindowstageevent9):
1021>
1022> 1. **on('windowStageEvent')** does not ensure the sequence of state transitions. It is not suitable for cases where the order of states matters. In such cases, use this API instead.
1023>
1024> 2. This API does not support listening for focus gain/loss states of the WindowStage. For such requirements, use [on('windowEvent')](arkts-apis-window-Window.md#onwindowevent10).
1025>
1026> 3. For details about system mechanisms and lifecycle transitions, see [Main Window Lifecycle](../../windowmanager/window-overview.md#main-window-lifecycle).
1027
1028**Model restriction**: This API can be used only in the stage model.
1029
1030**System capability**: SystemCapability.Window.SessionManager
1031
1032**Parameters**
1033
1034| Name  | Type                                                        | Mandatory| Description                                                        |
1035| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
1036| eventType  | string                                                       | Yes  | Event type. The value is fixed at **'windowStageLifecycleEvent'**, indicating the WindowStage lifecycle change event.|
1037| callback | Callback&lt;[WindowStageLifecycleEventType](arkts-apis-window-e.md#windowstagelifecycleeventtype20)&gt; | Yes  | Callback used to return the WindowStage lifecycle state.               |
1038
1039**Error codes**
1040
1041For details about the error codes, see [Window Error Codes](errorcode-window.md).
1042
1043| ID| Error Message|
1044| ------- | ------------------------------ |
1045| 801     | Capability not supported. Failed to call the API due to limited device capabilities. |
1046| 1300002 | This window state is abnormal. |
1047| 1300005 | This window stage is abnormal. |
1048
1049**Example**
1050
1051```ts
1052// EntryAbility.ets
1053import { UIAbility } from '@kit.AbilityKit';
1054
1055export default class EntryAbility extends UIAbility {
1056  // ...
1057
1058  onWindowStageCreate(windowStage: window.WindowStage) {
1059    console.info('onWindowStageCreate');
1060    const callback = (data: window.WindowStageLifecycleEventType) => {
1061      console.info('Succeeded in enabling the listener for window stage event changes. Data: ' +
1062        JSON.stringify(data));
1063      // Process services based on the event status.
1064      if (data === window.WindowStageLifecycleEventType.SHOWN) {
1065        console.info('current window stage event is SHOWN');
1066        // ...
1067      } else if (data === window.WindowStageLifecycleEventType.RESUMED) {
1068        console.info('current window stage event is RESUMED');
1069        // ...
1070      } else if (data === window.WindowStageLifecycleEventType.PAUSED) {
1071        console.info('current window stage event is PAUSED');
1072        // ...
1073      } else if (data === window.WindowStageLifecycleEventType.HIDDEN) {
1074        console.info('current window stage event is HIDDEN');
1075        // ...
1076      }
1077      // ...
1078    }
1079    try {
1080      windowStage.on('windowStageLifecycleEvent', callback);
1081    } catch (exception) {
1082      console.error(`Failed to enable the listener for window stage event changes. Cause code: ${exception.code}, message: ${exception.message}`);
1083    }
1084  }
1085};
1086```
1087
1088## off('windowStageLifecycleEvent')<sup>20+</sup>
1089
1090off(eventType: 'windowStageLifecycleEvent', callback?: Callback&lt;WindowStageLifecycleEventType&gt;): void
1091
1092Unsubscribes from the WindowStage lifecycle change event.
1093
1094**Model restriction**: This API can be used only in the stage model.
1095
1096**System capability**: SystemCapability.Window.SessionManager
1097
1098**Parameters**
1099
1100| Name  | Type                                                        | Mandatory| Description                                                        |
1101| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
1102| eventType  | string                                                       | Yes  | Event type. The value is fixed at **'windowStageLifecycleEvent'**, indicating the WindowStage lifecycle change event.|
1103| callback | Callback&lt;[WindowStageLifecycleEventType](arkts-apis-window-e.md#windowstagelifecycleeventtype20)&gt; | No  | Callback used to return the WindowStage lifecycle state. If a value is passed in, the corresponding subscription is canceled. If no value is passed in, all subscriptions to the specified event are canceled.               |
1104
1105**Error codes**
1106
1107For details about the error codes, see [Window Error Codes](errorcode-window.md).
1108
1109| ID| Error Message|
1110| ------- | ------------------------------ |
1111| 801     | Capability not supported. Failed to call the API due to limited device capabilities. |
1112| 1300002 | This window state is abnormal. |
1113| 1300005 | This window stage is abnormal. |
1114
1115**Example**
1116
1117```ts
1118// EntryAbility.ets
1119import { UIAbility } from '@kit.AbilityKit';
1120
1121export default class EntryAbility extends UIAbility {
1122  // ...
1123
1124  onWindowStageCreate(windowStage: window.WindowStage) {
1125    console.info('onWindowStageCreate');
1126    const callback = (windowStageLifecycleEvent: window.WindowStageLifecycleEventType) => {
1127      // ...
1128    }
1129    try {
1130      windowStage.on('windowStageLifecycleEvent', callback);
1131    } catch (exception) {
1132      console.error(`Failed to enable the listener for window stage event changes. Cause code: ${exception.code}, message: ${exception.message}`);
1133    }
1134    try {
1135      windowStage.off('windowStageLifecycleEvent', callback);
1136      // Unregister all the callbacks that have been registered through on().
1137      windowStage.off('windowStageLifecycleEvent');
1138    } catch (exception) {
1139      console.error(`Failed to disable the listener for window stage event changes. Cause code: ${exception.code}, message: ${exception.message}`);
1140    }
1141  }
1142};
1143```
1144
1145## on('windowStageClose')<sup>14+</sup>
1146
1147on(eventType: 'windowStageClose', callback: Callback&lt;void&gt;): void
1148
1149Subscribes to the click event on the close button in the three-button navigation bar of the main window. This API works only in [freeform window](../../windowmanager/window-terminology.md#freeform-window) mode. This event is triggered when the close button in the three-button navigation bar of the main window is clicked. The registered lifecycle callback function [UIAbility.onPrepareToTerminate](../apis-ability-kit/js-apis-app-ability-uiAbility.md#onpreparetoterminate10) is not executed.
1150
1151If the event is subscribed to multiple times, only the most recently subscribed-to event takes effect.
1152
1153The callback function in this API is executed synchronously. For asynchronous close events of the main window, refer to [on('windowWillClose')](arkts-apis-window-Window.md#onwindowwillclose15).
1154
1155If there is an existing event subscribed to by calling [on('windowWillClose')](arkts-apis-window-Window.md#onwindowwillclose15), only the [on('windowWillClose')](arkts-apis-window-Window.md#onwindowwillclose15) API will be responded to.
1156
1157**Model restriction**: This API can be used only in the stage model.
1158
1159**System capability**: SystemCapability.Window.SessionManager
1160
1161**Atomic service API**: This API can be used in atomic services since API version 14.
1162
1163**Device behavior differences**: This API can be properly called on 2-in-1 devices and tablets. If it is called on other device types, error code 801 is returned.
1164
1165**Parameters**
1166
1167| Name  | Type                                                        | Mandatory| Description                                                        |
1168| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
1169| eventType  | string                                                       | Yes  | Event type. The value is fixed at **'windowStageClose'**, indicating that the close button in the three-button navigation bar of the main window is clicked|
1170| callback | Callback&lt;void&gt; | Yes  | Callback invoked when the close button in the upper-right corner of the main window is clicked. It does not return any parameter. The internal logic of the callback function requires a return value of the Boolean type. The return value determines whether to continue to close the main window. The value **true** means not to close the main window, and **false** means to continue to close the main window.|
1171
1172**Error codes**
1173
1174For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Window Error Codes](errorcode-window.md).
1175
1176| ID| Error Message|
1177| ------- | ------------------------------ |
1178| 401     | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1179| 801 | Capability not supported. Failed to call the API due to limited device capabilities. |
1180| 1300002 | This window state is abnormal. |
1181
1182**Example**
1183
1184```ts
1185// EntryAbility.ets
1186import { UIAbility } from '@kit.AbilityKit';
1187import { window } from '@kit.ArkUI';
1188
1189export default class EntryAbility extends UIAbility {
1190  // ...
1191
1192  onWindowStageCreate(windowStage: window.WindowStage) {
1193    console.info('onWindowStageCreate');
1194    try {
1195      windowStage.on('windowStageClose', () => {
1196        console.info('Succeeded in enabling the listener for window stage close event.');
1197        return false;
1198      });
1199    } catch (exception) {
1200      console.error(`Failed to enable the listener for window stage close event. Cause code: ${exception.code}, message: ${exception.message}`);
1201    }
1202  }
1203};
1204```
1205
1206## off('windowStageClose')<sup>14+</sup>
1207
1208off(eventType: 'windowStageClose', callback?: Callback&lt;void&gt;): void
1209
1210Unsubscribes from the event indicating that the main window is closed. This API works only in [freeform window](../../windowmanager/window-terminology.md#freeform-window) mode.
1211
1212**Model restriction**: This API can be used only in the stage model.
1213
1214**System capability**: SystemCapability.Window.SessionManager
1215
1216**Atomic service API**: This API can be used in atomic services since API version 14.
1217
1218**Device behavior differences**: This API can be properly called on 2-in-1 devices and tablets. If it is called on other device types, error code 801 is returned.
1219
1220**Parameters**
1221
1222| Name  | Type                                                        | Mandatory| Description                                                        |
1223| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
1224| eventType  | string                                                       | Yes  | Event type. The value is fixed at **'windowStageClose'**, indicating that the close button in the three-button navigation bar of the main window is clicked.|
1225| callback | Callback&lt;void&gt; | No  | Callback invoked when the close button in the upper-right corner of the main window is clicked. It does not return any parameter. The internal logic of the callback function requires a return value of the Boolean type. The return value determines whether to continue to close the main window. The value **true** means not to close the main window, and **false** means to continue to close the main window. If a value is passed in, the corresponding subscription is canceled. If no value is passed in, all subscriptions to the specified event are canceled.|
1226
1227**Error codes**
1228
1229For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Window Error Codes](errorcode-window.md).
1230
1231| ID| Error Message|
1232| ------- | ------------------------------ |
1233| 401     | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1234| 801 | Capability not supported. Failed to call the API due to limited device capabilities. |
1235| 1300002 | This window state is abnormal. |
1236
1237**Example**
1238
1239```ts
1240// EntryAbility.ets
1241import { UIAbility } from '@kit.AbilityKit';
1242import { window } from '@kit.ArkUI';
1243
1244export default class EntryAbility extends UIAbility {
1245  // ...
1246
1247  onWindowStageCreate(windowStage: window.WindowStage) {
1248    console.info('onWindowStageCreate');
1249    const callback = () => {
1250      // ...
1251      return false;
1252    }
1253    try {
1254      windowStage.on('windowStageClose', callback);
1255      windowStage.off('windowStageClose', callback);
1256      windowStage.off('windowStageClose');
1257    } catch (exception) {
1258      console.error(`Failed to disable the listener for window stage close changes. Cause code: ${exception.code}, message: ${exception.message}`);
1259    }
1260  }
1261};
1262```
1263
1264## setDefaultDensityEnabled<sup>12+</sup>
1265
1266setDefaultDensityEnabled(enabled: boolean): void
1267
1268Sets whether the main window of the application uses the system's default density. Child windows and system windows will follow the main window's setting. Before calling this API, call [WindowStage.loadContent()](#loadcontent9) to initialize the layout to ensure the correct call sequence.
1269
1270If this API is not called, the default density is not used.
1271
1272When the default density is not used, if [setCustomDensity()](#setcustomdensity15) has been called, the window will be re-laid out according to the custom display size changes. Otherwise, it will be re-laid out according to the system display size changes.
1273
1274**Model restriction**: This API can be used only in the stage model.
1275
1276**Atomic service API**: This API can be used in atomic services since API version 12.
1277
1278**System capability**: SystemCapability.Window.SessionManager
1279
1280**Parameters**
1281
1282| Name          | Type   | Mandatory| Description                        |
1283| ---------------- | ------- | ---- | ---------------------------- |
1284| enabled | boolean | Yes  | Whether to enable the system's default density. **true** to enable, **false** otherwise. When the system's default density is enabled, the window layout does not change with the system display size.|
1285
1286**Error codes**
1287
1288For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Window Error Codes](errorcode-window.md).
1289
1290| ID| Error Message|
1291| ------- | ------------------------------ |
1292| 401     | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
1293| 801     | Capability not supported. Failed to call the API due to limited device capabilities. |
1294| 1300002 | This window state is abnormal. |
1295| 1300005 | This window stage is abnormal. |
1296
1297**Example**
1298
1299```ts
1300// EntryAbility.ets
1301import { UIAbility } from '@kit.AbilityKit';
1302import { window } from '@kit.ArkUI';
1303import { BusinessError } from '@kit.BasicServicesKit'
1304
1305export default class EntryAbility extends UIAbility {
1306  // ...
1307
1308  onWindowStageCreate(windowStage: window.WindowStage) {
1309      windowStage.loadContent("pages/page2", (err: BusinessError) => {
1310        let errCode: number = err.code;
1311        if (errCode) {
1312          console.error(`Failed to load the content. Cause code: ${err.code}, message: ${err.message}`);
1313          return;
1314        }
1315        console.info('onWindowStageCreate');
1316      try {
1317        windowStage.setDefaultDensityEnabled(true);
1318        console.info('Succeeded in loading the content.');
1319      } catch (exception) {
1320        console.error(`Failed to set default density enabled. Cause code: ${exception.code}, message: ${exception.message}`);
1321      }
1322    });
1323  }
1324};
1325```
1326
1327## setCustomDensity<sup>15+</sup>
1328
1329setCustomDensity(density: number): void
1330
1331Allows the main window of the application to customize its display size scale factor.
1332
1333Existing child windows and system windows do not immediately re-layout to match the main window's new scale factor. They will re-layout to reflect this change only when their layout information (such as position, size, and system scale size) changes.
1334
1335If both this API and [setDefaultDensityEnabled(true)](#setdefaultdensityenabled12) are called, the setting from the last called API will be applied.
1336
1337**Model restriction**: This API can be used only in the stage model.
1338
1339**Atomic service API**: This API can be used in atomic services since API version 15.
1340
1341**System capability**: SystemCapability.Window.SessionManager
1342
1343**Parameters**
1344
1345| Name          | Type   | Mandatory| Description                        |
1346| ---------------- | ------- | ---- | ---------------------------- |
1347| density | number | Yes  | Custom display size scale factor. The value is a floating-point number in the range [0.5, 4.0] or is set to **-1.0**. The value **4.0** indicates the largest permissible display size scale factor for the window, and **-1.0** means that the window uses the system's default display size scale factor.|
1348
1349**Error codes**
1350
1351For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Window Error Codes](errorcode-window.md).
1352
1353| ID| Error Message|
1354| ------- | ------------------------------ |
1355| 401     | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
1356| 801     | Capability not supported. Failed to call the API due to limited device capabilities. |
1357| 1300002 | This window state is abnormal. |
1358| 1300005 | This window stage is abnormal. |
1359
1360**Example**
1361
1362```ts
1363// EntryAbility.ets
1364import { UIAbility } from '@kit.AbilityKit';
1365
1366export default class EntryAbility extends UIAbility {
1367  // ...
1368
1369  onWindowStageCreate(windowStage: window.WindowStage) {
1370    console.info('onWindowStageCreate');
1371    try {
1372      windowStage.setCustomDensity(-1.0);
1373    } catch (exception) {
1374      console.error(`Failed to set custom density. Cause code: ${exception.code}, message: ${exception.message}`);
1375    }
1376  }
1377};
1378```
1379
1380## setCustomDensity<sup>20+</sup>
1381
1382setCustomDensity(density: number, applyToSubWindow?: boolean): void
1383
1384Allows the main window of the application to customize its display size scale factor and control when child windows and system windows re-layout to match the main window.
1385
1386If both this API and [setDefaultDensityEnabled(true)](#setdefaultdensityenabled12) are called, the setting from the last called API will be applied.
1387
1388**Model restriction**: This API can be used only in the stage model.
1389
1390**System capability**: SystemCapability.Window.SessionManager
1391
1392**Parameters**
1393
1394| Name          | Type   | Mandatory| Description                        |
1395| ---------------- | ------- | ---- | ---------------------------- |
1396| density | number | Yes  | Custom display size scale factor. The value is a floating-point number in the range [0.5, 4.0] or is set to **-1.0**. The value **4.0** indicates the largest permissible display size scale factor for the window, and **-1.0** means that the window uses the system's default display size scale factor.|
1397| applyToSubWindow | boolean | No  | Whether existing child windows and system windows should immediately re-layout to match the main window's new scale factor. If this parameter is set to **true**, they will immediately re-layout to match the main window's new scale factor. If this parameter is set to **false**, they will re-layout to reflect this change only when their layout information (such as position, size, and system scale size) changes. The default value is **false**.|
1398
1399**Error codes**
1400
1401For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Window Error Codes](errorcode-window.md).
1402
1403| ID| Error Message|
1404| ------- | ------------------------------ |
1405| 801     | Capability not supported. Failed to call the API due to limited device capabilities. |
1406| 1300002 | This window state is abnormal. |
1407| 1300005 | This window stage is abnormal. |
1408
1409**Example**
1410
1411```ts
1412// EntryAbility.ets
1413import { UIAbility } from '@kit.AbilityKit';
1414
1415export default class EntryAbility extends UIAbility {
1416  // ...
1417
1418  onWindowStageCreate(windowStage: window.WindowStage) {
1419    console.info('onWindowStageCreate');
1420    try {
1421      windowStage.setCustomDensity(2.0);
1422      windowStage.setCustomDensity(3.0, true);
1423      windowStage.setCustomDensity(-1.0, false);
1424    } catch (exception) {
1425      console.error(`Failed to set custom density. Cause code: ${exception.code}, message: ${exception.message}`);
1426    }
1427  }
1428};
1429```
1430
1431## setWindowModal<sup>14+</sup>
1432
1433setWindowModal(isModal: boolean): Promise&lt;void&gt;
1434
1435Enables the modal property of the main window. This API uses a promise to return the result. It works only in [freeform window](../../windowmanager/window-terminology.md#freeform-window) mode.
1436
1437This API must be called by the main window and the setting takes effect for the main window. After the modal property of the main window is enabled, other main windows in the same application process and their child windows do not respond to user interactions until the main window is closed or the main window's modal property is disabled.
1438
1439**Model restriction**: This API can be used only in the stage model.
1440
1441**Atomic service API**: This API can be used in atomic services since API version 14.
1442
1443**System capability**: SystemCapability.Window.SessionManager
1444
1445**Device behavior differences**: This API can be properly called on 2-in-1 devices and tablets. If it is called on other device types, error code 801 is returned.
1446
1447**Parameters**
1448
1449| Name   | Type   | Mandatory| Description                                         |
1450| --------- | ------- | ---- | --------------------------------------------- |
1451| isModal | boolean | Yes  | Whether to enable the modal property of the main window. **true** to enable, **false** otherwise.|
1452
1453
1454**Return value**
1455
1456| Type| Description|
1457| ------------------- | ------------------------ |
1458| Promise&lt;void&gt; | Promise that returns no value.|
1459
1460**Error codes**
1461
1462For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Window Error Codes](errorcode-window.md).
1463
1464| ID| Error Message                      |
1465| -------- | ------------------------------ |
1466| 401      | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
1467| 801      | Capability not supported. Failed to call the API due to limited device capabilities. |
1468| 1300002  | This window state is abnormal. |
1469| 1300003  | This window manager service works abnormally. |
1470| 1300005 | This window stage is abnormal. |
1471
1472**Example**
1473
1474```ts
1475// EntryAbility.ets
1476import { UIAbility } from '@kit.AbilityKit';
1477import { BusinessError } from '@kit.BasicServicesKit';
1478
1479export default class EntryAbility extends UIAbility {
1480  // ...
1481  onWindowStageCreate(windowStage: window.WindowStage): void {
1482    console.info('onWindowStageCreate');
1483    try {
1484      let promise = windowStage.setWindowModal(true);
1485      promise.then(() => {
1486        console.info('Succeeded in setting window modal');
1487      }).catch((err: BusinessError) => {
1488        console.error(`Failed to set window modal. Cause code: ${err.code}, message: ${err.message}`);
1489      });
1490    } catch (exception) {
1491      console.error(`Failed to set window modal. Cause code: ${exception.code}, message: ${exception.message}`);
1492    }
1493  }
1494}
1495```
1496
1497## removeStartingWindow<sup>14+</sup>
1498
1499removeStartingWindow(): Promise&lt;void&gt;
1500
1501Allows the application to control the time when the launch page disappears.
1502
1503This API takes effect only for the application main window when **enable.remove.starting.window** under **metadata** in [abilities](../../quick-start/module-configuration-file.md#abilities) in the **module.json5** file is set to **true**.
1504
1505If the tag is set to **true**, the system provides timeout protection for the launch page. If this API is not called within 5 seconds, the system automatically removes the launch page.
1506
1507If the tag is set to **false** or is not configured, this API does not take effect. The launch page will be automatically removed after the first frame of the application is rendered.
1508
1509**Model restriction**: This API can be used only in the stage model.
1510
1511**Atomic service API**: This API can be used in atomic services since API version 14.
1512
1513**System capability**: SystemCapability.Window.SessionManager
1514
1515**Return value**
1516
1517| Type               | Description                     |
1518| ------------------- | ------------------------- |
1519| Promise&lt;void&gt; | Promise that returns no value.|
1520
1521**Error codes**
1522
1523For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Window Error Codes](errorcode-window.md).
1524
1525| ID| Error Message|
1526| ------- | ------------------------------ |
1527| 801     | Capability not supported. Failed to call the API due to limited device capabilities. |
1528| 1300002 | This window state is abnormal. |
1529| 1300003 | This window manager service works abnormally. |
1530
1531**Example**
1532
1533```ts
1534// EntryAbility.ets
1535import { UIAbility } from '@kit.AbilityKit';
1536import { BusinessError } from '@kit.BasicServicesKit';
1537
1538export default class EntryAbility extends UIAbility {
1539  // ...
1540
1541  onWindowStageCreate(windowStage: window.WindowStage) {
1542    console.info('onWindowStageCreate');
1543    windowStage.removeStartingWindow().then(() => {
1544      console.info('Succeeded in removing starting window.');
1545    }).catch((err: BusinessError) => {
1546        console.error(`Failed to remove starting window. Cause code: ${err.code}, message: ${err.message}`);
1547    });
1548  }
1549};
1550```
1551
1552## setWindowRectAutoSave<sup>14+</sup>
1553
1554setWindowRectAutoSave(enabled: boolean): Promise&lt;void&gt;
1555
1556Enables or disables the auto-save feature for the size of the last closed main window. This API uses a promise to return the result.
1557
1558When the auto-save feature is enabled, within the same UIAbility, the size of the last closed main window is remembered. When this main window is restarted, it will open at the remembered size according to the rules.
1559
1560The following rules apply in stacking scenarios:
1561
15621. If the current instance is a free-form window, the next opened window will adopt the same size when stacked.
15632. If the current instance is maximized or in full-screen mode, the next opened window will maintain the maximized state when stacked.
1564
1565The following table describes the memory rules:
1566
1567|Last Window State|Memorizing Rule|
1568|-------------|-------|
1569|Free-form window|Retains the size/position of the free-form window, rebound if exceeding the workspace.|
1570|Split-screen window|Retains the size and position of the free-form window before screen splitting.|
1571|Maximized window|Retains the maximized state.|
1572|Immersive window|Retains the size and position of the free-form window before immersive mode.|
1573|Minimized window|Retains the size and position of the free-form window before minimization.|
1574
1575**Model restriction**: This API can be used only in the stage model.
1576
1577**Atomic service API**: This API can be used in atomic services since API version 14.
1578
1579**System capability**: SystemCapability.Window.SessionManager
1580
1581**Device behavior differences**: This API can be properly called on 2-in-1 devices. If it is called on other device types, error code 801 is returned.
1582
1583**Parameters**
1584
1585| Name   | Type   | Mandatory| Description                                         |
1586| --------- | ------- | ---- | --------------------------------------------- |
1587| enabled | boolean | Yes  | Whether to enable the auto-save feature. **true** to enable, **false** otherwise.|
1588
1589
1590**Return value**
1591
1592| Type| Description|
1593| ------------------- | ------------------------ |
1594| Promise&lt;void&gt; | Promise that returns no value.|
1595
1596**Error codes**
1597
1598For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Window Error Codes](errorcode-window.md).
1599
1600| ID| Error Message                      |
1601| -------- | ------------------------------ |
1602| 401      | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
1603| 801      | Capability not supported. Failed to call the API due to limited device capabilities. |
1604| 1300002  | This window state is abnormal. |
1605| 1300003  | This window manager service works abnormally. |
1606
1607**Example**
1608
1609```ts
1610// EntryAbility.ets
1611import { UIAbility } from '@kit.AbilityKit';
1612import { BusinessError } from '@kit.BasicServicesKit';
1613
1614export default class EntryAbility extends UIAbility {
1615  // ...
1616  onWindowStageCreate(windowStage: window.WindowStage): void {
1617    console.info('onWindowStageCreate');
1618    try {
1619      let promise = windowStage.setWindowRectAutoSave(true);
1620      promise.then(() => {
1621        console.info('Succeeded in setting window rect auto-save');
1622      }).catch((err: BusinessError) => {
1623        console.error(`Failed to set window rect auto-save. Cause code: ${err.code}, message: ${err.message}`);
1624      });
1625    } catch (exception) {
1626      console.error(`Failed to set window rect auto-save. Cause code: ${exception.code}, message: ${exception.message}`);
1627    }
1628  }
1629}
1630```
1631
1632## setWindowRectAutoSave<sup>17+</sup>
1633
1634setWindowRectAutoSave(enabled: boolean, isSaveBySpecifiedFlag: boolean): Promise&lt;void&gt;
1635
1636Enables or disables the auto-save feature for the size of the main window. This API uses a promise to return the result.
1637
1638Within the same UIAbility, you can remember the size of the last closed main window, or you can remember the size of each main window individually. The individual auto-save feature for each main window size is only available when the UIAbility launch mode is set to **specified** and the **isSaveBySpecifiedFlag** parameter is set to **true**.
1639
1640When the auto-save feature is enabled, the size of the main window when it is closed is remembered. When the main window is restarted, it will open at the remembered size according to the rules.
1641
1642The following table describes the memory rules:
1643
1644|Last Window State|Memorizing Rule|
1645|-------------|-------|
1646|Free-form window|Retains the size/position of the free-form window, rebound if exceeding the workspace.|
1647|Split-screen window|Retains the size and position of the free-form window before screen splitting.|
1648|Maximized window|Retains the maximized state.|
1649|Immersive window|Retains the size and position of the free-form window before immersive mode.|
1650|Minimized window|Retains the size and position of the free-form window before minimization.|
1651
1652**Model restriction**: This API can be used only in the stage model.
1653
1654**Atomic service API**: This API can be used in atomic services since API version 17.
1655
1656**System capability**: SystemCapability.Window.SessionManager
1657
1658**Device behavior differences**: This API can be properly called on 2-in-1 devices. If it is called on other device types, error code 801 is returned.
1659
1660**Parameters**
1661
1662| Name   | Type   | Mandatory| Description                                         |
1663| --------- | ------- | ---- | --------------------------------------------- |
1664| enabled | boolean | Yes  | Whether to enable the auto-save feature. **true** to enable, **false** otherwise.|
1665| isSaveBySpecifiedFlag | boolean | Yes  | Whether to enable the individual auto-save feature when the UIAbility launch mode is set to **specified**. **true** to enable, **false** otherwise.|
1666
1667
1668**Return value**
1669
1670| Type| Description|
1671| ------------------- | ------------------------ |
1672| Promise&lt;void&gt; | Promise that returns no value.|
1673
1674**Error codes**
1675
1676For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Window Error Codes](errorcode-window.md).
1677
1678| ID| Error Message                      |
1679| -------- | ------------------------------ |
1680| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
1681| 801      | Capability not supported. Function setWindowRectAutoSave can not work correctly due to limited device capabilities. |
1682| 1300002  | This window state is abnormal. |
1683| 1300003  | This window manager service works abnormally. |
1684
1685**Example**
1686
1687```ts
1688// EntryAbility.ets
1689import { UIAbility } from '@kit.AbilityKit';
1690import { BusinessError } from '@kit.BasicServicesKit';
1691
1692export default class EntryAbility extends UIAbility {
1693  // ...
1694  onWindowStageCreate(windowStage: window.WindowStage): void {
1695    console.info('onWindowStageCreate');
1696    try {
1697      let promise = windowStage.setWindowRectAutoSave(true, true);
1698      promise.then(() => {
1699        console.info('Succeeded in setting window rect auto-save');
1700      }).catch((err: BusinessError) => {
1701        console.error(`Failed to set window rect auto-save. Cause code: ${err.code}, message: ${err.message}`);
1702      });
1703    } catch (exception) {
1704      console.error(`Failed to set window rect auto-save. Cause code: ${exception.code}, message: ${exception.message}`);
1705    }
1706  }
1707}
1708```
1709
1710## isWindowRectAutoSave<sup>14+</sup>
1711
1712isWindowRectAutoSave(): Promise&lt;boolean&gt;
1713
1714Checks whether the auto-save feature is enabled for the main window's size. This API uses a promise to return the result.
1715
1716**Model restriction**: This API can be used only in the stage model.
1717
1718**Atomic service API**: This API can be used in atomic services since API version 14.
1719
1720**System capability**: SystemCapability.Window.SessionManager
1721
1722**Device behavior differences**: This API can be properly called on 2-in-1 devices. If it is called on other device types, error code 801 is returned.
1723
1724**Return value**
1725
1726| Type| Description|
1727| ---------------------- | ------------------------------------------------------------------------------------ |
1728| Promise&lt;boolean&gt; | Promise used to return the result. **true** if enabled, **false** otherwise.|
1729
1730**Error codes**
1731
1732For details about the error codes, see [Window Error Codes](errorcode-window.md).
1733
1734| ID| Error Message|
1735| ------- | ------------------------------ |
1736| 801      | Capability not supported. Failed to call the API due to limited device capabilities. |
1737| 1300002 | This window state is abnormal. |
1738| 1300003 | This window manager service works abnormally. |
1739
1740**Example**
1741
1742```ts
1743// EntryAbility.ets
1744import { UIAbility } from '@kit.AbilityKit';
1745import { BusinessError } from '@kit.BasicServicesKit';
1746
1747export default class EntryAbility extends UIAbility {
1748  // ...
1749  onWindowStageCreate(windowStage: window.WindowStage): void {
1750    console.info('onWindowStageCreate');
1751    try {
1752      let promise = windowStage.isWindowRectAutoSave();
1753      promise.then((data) => {
1754        console.info(`Succeeded in checking whether the window support the rect auto-save. Data: ${data}`);
1755      }).catch((err: BusinessError) => {
1756        console.error(`Failed to check whether the window support the rect auto-save. Cause code: ${err.code}, message: ${err.message}`);
1757      });
1758    } catch (exception) {
1759      console.error(`Failed to check whether the window support the rect auto-save. Cause code: ${exception.code}, message: ${exception.message}`);
1760    }
1761  }
1762}
1763```
1764
1765## setSupportedWindowModes<sup>15+</sup>
1766
1767setSupportedWindowModes(supportedWindowModes: Array<bundleManager.SupportWindowMode>): Promise&lt;void&gt;
1768
1769Sets the supported window modes of the main window. This API uses a promise to return the result. It works only in [freeform window](../../windowmanager/window-terminology.md#freeform-window) mode.
1770
1771**Model restriction**: This API can be used only in the stage model.
1772
1773**Atomic service API**: This API can be used in atomic services since API version 15.
1774
1775**System capability**: SystemCapability.Window.SessionManager
1776
1777**Device behavior differences**: This API can be properly called on 2-in-1 devices and tablets. If it is called on other device types, error code 801 is returned.
1778
1779**Parameters**
1780
1781| Name   | Type   | Mandatory| Description                                         |
1782| --------- | ------- | ---- | --------------------------------------------- |
1783| supportedWindowModes | Array&lt;[bundleManager.SupportWindowMode](../apis-ability-kit/js-apis-bundleManager.md#supportwindowmode)&gt; | Yes  | Supported window modes.<br>- **FULL_SCREEN**: full-screen mode.<br>- **FLOATING**: floating window mode.<br>- **SPLIT**: split-screen mode. **FULL_SCREEN** or **FLOATING** must be used together. Configuring only **SPLIT** is not supported.<br> Note: The values of the **SupportWindowMode** field in the array should not conflict with the values of the **supportWindowMode** field under [abilities](../../quick-start/module-configuration-file.md#abilities) of the [module.json5 file](../../quick-start/module-configuration-file.md) corresponding to this UIAbility, or with the values of the **supportWindowModes** field in [StartOptions](../apis-ability-kit/js-apis-app-ability-startOptions.md#startoptions). In case of a conflict, the window support mode set by this parameter will take precedence.|
1784
1785**Return value**
1786
1787| Type| Description|
1788| ------------------- | ------------------------ |
1789| Promise&lt;void&gt; | Promise that returns no value.|
1790
1791**Error codes**
1792
1793For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Window Error Codes](errorcode-window.md).
1794
1795| ID| Error Message                      |
1796| -------- | ------------------------------ |
1797| 401      | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
1798| 801      | Capability not supported. Failed to call the API due to limited device capabilities. |
1799| 1300002  | This window state is abnormal. |
1800| 1300003  | This window manager service works abnormally. |
1801
1802**Example**
1803
1804```ts
1805// EntryAbility.ets
1806import { UIAbility, bundleManager } from '@kit.AbilityKit';
1807import { BusinessError } from '@kit.BasicServicesKit';
1808
1809export default class EntryAbility extends UIAbility {
1810  // ...
1811  onWindowStageCreate(windowStage: window.WindowStage): void {
1812    console.info('onWindowStageCreate');
1813    try {
1814      let promise = windowStage.setSupportedWindowModes([
1815        bundleManager.SupportWindowMode.FULL_SCREEN,
1816        bundleManager.SupportWindowMode.SPLIT,
1817        bundleManager.SupportWindowMode.FLOATING
1818      ]);
1819      promise.then(() => {
1820        console.info('Succeeded in setting window support modes');
1821      }).catch((err: BusinessError) => {
1822        console.error(`Failed to set window support modes. Cause code: ${err.code}, message: ${err.message}`);
1823      });
1824    } catch (exception) {
1825      console.error(`Failed to set window support modes. Cause code: ${exception.code}, message: ${exception.message}`);
1826    }
1827  }
1828}
1829```
1830
1831## setSupportedWindowModes<sup>20+</sup>
1832
1833setSupportedWindowModes(supportedWindowModes: Array<bundleManager.SupportWindowMode>, grayOutMaximizeButton: boolean): Promise&lt;void&gt;
1834
1835Sets the supported window modes for the main window and optionally disables the maximize button. This API uses a promise to return the result. It takes effect only in [freeform window](../../windowmanager/window-terminology.md#freeform-window) mode.
1836
1837**Model restriction**: This API can be used only in the stage model.
1838
1839**System capability**: SystemCapability.Window.SessionManager
1840
1841**Device behavior differences**: This API can be properly called on 2-in-1 devices and tablets. If it is called on other device types, error code 801 is returned.
1842
1843**Parameters**
1844
1845| Name   | Type   | Mandatory| Description                                         |
1846| --------- | ------- | ---- | --------------------------------------------- |
1847| supportedWindowModes | Array&lt;[bundleManager.SupportWindowMode](../apis-ability-kit/js-apis-bundleManager.md#supportwindowmode)&gt; | Yes  | Supported window modes.<br>- **FULL_SCREEN**: full-screen mode.<br>- **FLOATING**: floating window mode.<br>- **SPLIT**: split-screen mode. **FULL_SCREEN** or **FLOATING** must be used together. Configuring only **SPLIT** is not supported.<br> Note: The values of the **SupportWindowMode** field in the array should not conflict with the value of the **supportWindowMode** field under [abilities](../../quick-start/module-configuration-file.md#abilities) or **supportWindowModes** field in [StartOptions](../apis-ability-kit/js-apis-app-ability-startOptions.md#startoptions) of the [module.json5 file](../../quick-start/module-configuration-file.md) corresponding to this UIAbility. In case of a conflict, the window support mode set by this parameter will take precedence.|
1848| grayOutMaximizeButton | boolean | Yes| Whether to display the main window and disable its maximize button. The value **true** means to display the main window and disable its maximize button, and **false** means the opposite. This parameter takes effect only when **supportedWindowModes** is not set to **FULL_SCREEN**.|
1849
1850**Return value**
1851
1852| Type| Description|
1853| ------------------- | ------------------------ |
1854| Promise&lt;void&gt; | Promise that returns no value.|
1855
1856**Error codes**
1857
1858For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Window Error Codes](errorcode-window.md).
1859
1860| ID| Error Message                      |
1861| -------- | ------------------------------ |
1862| 801      | Capability not supported. Function setSupportedWindowModes can not work correctly due to limited device capabilities. |
1863| 1300002  | This window state is abnormal. |
1864| 1300003  | This window manager service works abnormally. |
1865| 1300016  | Parameter error. Possible cause: 1. Invalid parameter range. 2. Invalid parameter length. 3. Incorrect parameter format. |
1866
1867**Example**
1868
1869```ts
1870// EntryAbility.ets
1871import { UIAbility, bundleManager } from '@kit.AbilityKit';
1872import { BusinessError } from '@kit.BasicServicesKit';
1873
1874export default class EntryAbility extends UIAbility {
1875  // ...
1876  onWindowStageCreate(windowStage: window.WindowStage): void {
1877    console.info('onWindowStageCreate');
1878    try {
1879      let promise = windowStage.setSupportedWindowModes([
1880        bundleManager.SupportWindowMode.FULL_SCREEN,
1881        bundleManager.SupportWindowMode.SPLIT,
1882        bundleManager.SupportWindowMode.FLOATING
1883      ], true);
1884      promise.then(() => {
1885        console.info('Succeeded in setting window support modes');
1886      }).catch((err: BusinessError) => {
1887        console.error(`Failed to set window support modes. Cause code: ${err.code}, message: ${err.message}`);
1888      });
1889    } catch (exception) {
1890      console.error(`Failed to set window support modes. Cause code: ${exception.code}, message: ${exception.message}`);
1891    }
1892  }
1893}
1894```
1895