HarmonyOS 鸿蒙Next 请问获取首选项使用onPageShow存的时候不能实时更新 第二次打开应用必现
HarmonyOS 鸿蒙Next 请问获取首选项使用onPageShow存的时候不能实时更新 第二次打开应用必现
import { preferences } from ‘@kit.ArkData’;
import { UIAbility } from ‘@kit.AbilityKit’;
import { BusinessError } from ‘@kit.BasicServicesKit’;
import { window } from ‘@kit.ArkUI’;
import { util } from ‘@kit.ArkTS’;
let dataPreferences: preferences.Preferences | null = null;
let options: preferences.Options = { name: ‘myStore’ };
dataPreferences = preferences.getPreferencesSync(getContext(this), options);
@Observed
export class Language {
lanager: string;
zhName: string;
speek: boolean;
lyon: boolean; // 蓝牙
skon: boolean; // 扬声器
constructor(lanager: string,zhName: string, speek: boolean, lyon: boolean, skon: boolean) {
this.lanager = lanager;
this.zhName = zhName;
this.speek = speek;
this.lyon = lyon;
this.skon = skon;
}
}
@Component
struct LanguageItem {
@ObjectLink item: Language;
@Consume languageList: Language[]
build() {
Row() {
Text(this.item.lanager)
.fontColor(’#fff’)
if (this.item.speek) {
Image($r(‘app.media.translate_play’))
.width(30)
}
}
.backgroundColor(’#2B82B4’)
.height(20)
.borderRadius(5)
.padding({
left: 5,
right: this.item.speek ? 0 : 5
})
.onClick(() => {
this.item.speek = !this.item.speek
console.log(‘语言数组-存’,JSON.stringify(this.languageList))
// PreferencesUtil.putPreferenceValue(‘MyPreferences’, ‘languageList’, JSON.stringify(this.languageList))
// 此处以此键值对不存在时写入数据为例
if (dataPreferences) {
dataPreferences.putSync(‘startup’,JSON.stringify(this.languageList));
dataPreferences.flush((err: BusinessError) => {
if (err) {
console.error(Failed to flush. Code:${err.code}, message:${err.message}
);
return;
}
console.info(‘Succeeded in flushing.’);
})
}
})
}
}
@Entry
@Component
struct Parent {
@Provide languageList: Language[] =
[new Language(‘SYS’, ‘跟随系统’, true, false, true), new Language(‘EN’, ‘英语’, true, false, true)];
async onPageShow(): Promise<void> {
if (dataPreferences) {
let languageListTem = dataPreferences.getSync(‘startup’, ‘default’) as string;
console.info("The ‘startup’ value is " + languageListTem);
// const languageListTem = await PreferencesUtil.getPreferenceValue(‘MyPreferences’, ‘languageList’, ‘’) as string;
if(languageListTem){
const tem: Language[] = JSON.parse(languageListTem);
this.languageList = tem.map((item: Language) => {
return new Language(item.lanager,item.zhName,item.speek,item.lyon,item.skon);
})
}
console.log(‘语言数组’,JSON.stringify(this.languageList))
}
}
build() {
Column(){
Row({ space: 3 }) {
ForEach(this.languageList, (item: Language) => {
LanguageItem({
item: item
})
}, (item: Language) => JSON.stringify(item))
}
.margin({
top: 50
})
}
}
}
试下这个demo
import { preferences } from '@kit.ArkData';
import { UIAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { window } from '@kit.ArkUI';
import { util } from '@kit.ArkTS';
let dataPreferences: preferences.Preferences | null = null;
let options: preferences.Options = { name: ‘myStore’ };
dataPreferences = preferences.getPreferencesSync(getContext(), options);
@Observed
export class Language {
lanager: string;
zhName: string;
speek: boolean;
lyon: boolean; // 蓝牙
skon: boolean; // 扬声器
constructor(lanager: string, zhName: string, speek: boolean, lyon: boolean, skon: boolean) {
this.lanager = lanager;
this.zhName = zhName;
this.speek = speek;
this.lyon = lyon;
this.skon = skon;
}
}
@Component
struct LanguageItem {
@ObjectLink item: Language;
@Consume languageList: Language[]
build() {
Row() {
Text(this.item.lanager)
.fontColor(’#fff’)
if (this.item.speek) {
Image($r(‘app.media.img’))
.width(30)
}
}
.backgroundColor(’#2B82B4’)
.height(20)
.borderRadius(5)
.padding({
left: 5,
right: this.item.speek ? 0 : 5
})
.onClick(() => {
console.log(’’ + !this.item.speek)
// this.item.speek = !this.item.speek
if (this.item.lanager == ‘SYS’){
this.item.speek = !this.item.speek
this.languageList[0].speek = this.item.speek
}else if (this.item.lanager == ‘EN’){
this.item.speek = !this.item.speek
this.languageList[1].speek =this.item.speek
}
console.log(‘语言数组-存’,
JSON.stringify(this.languageList[0].speek) + JSON.stringify(this.languageList[1].speek))
// PreferencesUtil.putPreferenceValue(‘MyPreferences’, ‘languageList’, JSON.stringify(this.languageList))
// 此处以此键值对不存在时写入数据为例
if (dataPreferences) {
dataPreferences.putSync(‘startup’, JSON.stringify(this.languageList));
dataPreferences.flush((err: BusinessError) => {
if (err) {
console.error(Failed to flush. Code:${err.code}, message:${err.message});
return;
}
// console.info(‘Succeeded in flushing.’);
})
}
})
}
}
@Entry
@Component
struct Parent {
@Provide languageList: Language[] =
[new Language(‘SYS’, ‘跟随系统’, true, false, true), new Language(‘EN’, ‘英语’, true, false, true)];
async onPageShow(): Promise {
if (dataPreferences) {
let languageListTem = dataPreferences.getSync(‘startup’, null) as string;
console.info("The ‘startup’ value is " + languageListTem);
// const languageListTem = await PreferencesUtil.getPreferenceValue(‘MyPreferences’, ‘languageList’, ‘’) as string;
if (languageListTem) {
const tem: Language[] = JSON.parse(languageListTem);
this.languageList = tem.map((item: Language) => {
return new Language(item.lanager, item.zhName, item.speek, item.lyon, item.skon);
})
}
console.log(‘语言数组’, JSON.stringify(this.languageList[0].speek) + JSON.stringify(this.languageList[1].speek))
}
}
build() {
Column() {
Row({ space: 3 }) {
ForEach(this.languageList, (item: Language) => {
LanguageItem({
item: item
})
}, (item: Language) => JSON.stringify(item))
}
.margin({
top: 50
})
}
}
}
针对HarmonyOS 鸿蒙Next中获取首选项在onPageShow
时不能实时更新,第二次打开应用必现的问题,这通常与数据访问权限、数据存在性、生命周期管理以及存储机制有关。以下是一些可能的解决方案:
- 数据访问权限:确保应用已获取访问首选项数据的权限。在鸿蒙系统中,权限管理较为严格,未授权可能导致数据访问失败。
- 数据存在性:检查所需数据是否确实已正确存储在首选项中。如果数据不存在或存储格式错误,获取操作将无法返回有效值。
- 生命周期管理:
onPageShow
可能不是在数据更新后立即触发的。考虑使用其他生命周期函数(如aboutToAppear
)或在数据更新后手动触发更新逻辑。 - 存储机制:确认使用的存储机制(如Preferences)是否符合预期,并检查其生命周期和访问范围。
此外,尝试清除缓存并重启应用,检查并调整数据加载逻辑,确保在适当的生命周期阶段加载数据。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html