diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 0783712dbc53a144b73cc496392a38a89320ae02..64f2a18d3649ad8b87e4f311811a1d0031ce56e7 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -43,7 +43,14 @@ export default class EntryAbility extends UIAbility { } onWindowStageCreate(windowStage: window.WindowStage): void { - windowStage.loadContent('pages/Index', this.storage); + windowStage.loadContent('pages/Index', this.storage, (err, data) => { + AppStorage.setOrCreate('uiContext', windowStage.getMainWindowSync().getUIContext()); + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); + }); } async getCache(): Promise { diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index ea872e97a80de342aa6cb2630cc8ab656e6f21c3..cb4e958581266a84250b8f56049215005cabfd26 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -16,7 +16,8 @@ import { FULL_HEIGHT, FULL_WIDTH } from '../common/constant/CommonConstants'; import TabBar from '../view/TabBar'; -let storage = LocalStorage.getShared(); +const uiContext: UIContext | undefined = AppStorage.get('uiContext'); +const storage = uiContext?.getSharedLocalStorage() @Entry(storage) @Component diff --git a/entry/src/main/ets/view/ListPage.ets b/entry/src/main/ets/view/ListPage.ets index 9881bd8964d3f1f45b08b0a49f5e46e841de1ff4..815f9f461c74c4bb6d3bc9ff28f17f52feb2f1ad 100644 --- a/entry/src/main/ets/view/ListPage.ets +++ b/entry/src/main/ets/view/ListPage.ets @@ -18,6 +18,7 @@ import Item from './NewsItem'; import ListModel from '../model/ListModel'; import { ResData, Refresh } from '../common/types/CommonTypes'; import { getUrl, loadMore, refreshing, refreshState } from '../common/utils/Refresh'; +import { common } from '@kit.AbilityKit'; @Component export default struct ListPage { @@ -25,7 +26,7 @@ export default struct ListPage { @State newsModel: ListModel = new ListModel(); @Watch('getInitData') @LocalStorageLink('List') List: ResData[] = []; @LocalStorageLink('Fail') isFail: boolean = false; - context = getContext(); + context = this.getUIContext().getHostContext() as common.UIAbilityContext; getInitData() { this.newsModel.currentPage = 1; diff --git a/entry/src/main/ets/view/TabBar.ets b/entry/src/main/ets/view/TabBar.ets index 1e340660178d8e4bec6eb54632105c3490a9a988..28c901db6c9a12f5bee330e232cb1ec8b949dee1 100644 --- a/entry/src/main/ets/view/TabBar.ets +++ b/entry/src/main/ets/view/TabBar.ets @@ -13,11 +13,11 @@ * limitations under the License. */ -import { promptAction } from '@kit.ArkUI'; import { DataCache } from '@hadss/datacache'; import ListPage from './ListPage'; import * as Const from '../common/constant/CommonConstants'; import { TabType } from '../common/types/CommonTypes'; +import { common } from '@kit.AbilityKit'; @Component export default struct TabBar { @@ -25,6 +25,7 @@ export default struct TabBar { @State currentIndex: number = 0; @State currentPage: number = 1; private controller: TabsController = new TabsController(); + context = this.getUIContext().getHostContext() as common.UIAbilityContext; build() { Column() { @@ -52,8 +53,8 @@ export default struct TabBar { .fontSize(Const.CLEAR_FONT_SIZE) .fontWeight(Const.CLEAR_FONT_WEIGHT) .onClick(() => { - DataCache.getInstance(getContext(this)).clear().then(() => { - promptAction.showToast({ + DataCache.getInstance(this.context).clear().then(() => { + this.getUIContext().getPromptAction().showToast({ message: $r('app.string.clear_success'), duration: Const.ANIMATION_DURATION });