diff --git a/entry/src/main/ets/common/utils/CommonUtils.ets b/entry/src/main/ets/common/utils/CommonUtils.ets index f955f172c8e1230ec73d1d28177d33593bc63744..56372f726d9de849853c80907d7f336eebf39149 100644 --- a/entry/src/main/ets/common/utils/CommonUtils.ets +++ b/entry/src/main/ets/common/utils/CommonUtils.ets @@ -13,16 +13,21 @@ * limitations under the License. */ -import { promptAction } from '@kit.ArkUI'; import { CommonConstants } from '../constants/Constants'; -const uiContext: UIContext | undefined = AppStorage.get('uiContext'); +import Logger from './Logger'; export class CommonUtils { showToast(message: string) { - uiContext!.getPromptAction().showToast({ - message: message, - duration: CommonConstants.TOAST_TIME - }); + const uiContext: UIContext | undefined = AppStorage.get('uiContext'); + try { + uiContext!.getPromptAction().showToast({ + message: message, + duration: CommonConstants.TOAST_TIME + }); + } catch (error) { + let err = error as BusinessError; + Logger.error('CommonUtils', `Show toast failed, errCode: ${err.code}, errMessage: ${err.message}.`); + } } } diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 99ed4d2419c062f7cdf6737e97841f6e3945896f..631333d05052079c193779de35b8a9b062e64f1c 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -36,7 +36,6 @@ export default class EntryAbility extends UIAbility { return; } hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); - AppStorage.setOrCreate('uiContext', windowStage.getMainWindowSync().getUIContext()); }); } diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index b8c71cad6cdb8cee5e37f900dbd931d8e3863230..6b136b775b0b903477f44a613693e537beb45fe9 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -51,6 +51,10 @@ struct Index { offset: { dx: CommonConstants.OFFSET_X, dy: CommonConstants.OFFSET_Y } }); + aboutToAppear(): void { + AppStorage.setOrCreate('uiContext', this.getUIContext()); + } + build() { Column() { Text($r('app.string.ArkTS_Development_Case'))