通过首选项保存Toggle组件的值,应用重新启动后Toggle的值和全局变量不一致。导致EntryAbility中无法正确获取到Toggle组件的值,该如何解决?
问题代码如下:
onBackground(): void {
// Ability has back to background
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground');
// 判断是否销毁应用
if (delAppValue === true) {
// module.json5配置文件中将removeMissionAfterTerminate字段取值配置为true
this.context.terminateSelf();
}
} import { preferences } from '@kit.ArkData';
import { PromptAction } from '@kit.ArkUI';
export let delAppValue: boolean = false; // delAppValue初始化默认值
let preferenceDelApp: preferences.Preferences | null = null;
@Entry
@Component
struct SetPage {
@State delApp: boolean = delAppValue;
uiContext: UIContext = this.getUIContext();
context: Context = this.uiContext.getHostContext() as Context;
promptAction: PromptAction = this.uiContext.getPromptAction();
async aboutToAppear() {
delAppValue = await this.getPreferenceDelApp(); // 获取首选项中的值,同步到全局变量
this.promptAction.showToast({ message: String(delAppValue) });
}
// 获取首选项中DelApp的值
async getPreferenceDelApp(): Promise<boolean> {
let value: boolean = false;
try {
preferenceDelApp = await preferences.getPreferences(this.context, 'DelAppID');
value = await preferenceDelApp.get('DelAppID', delAppValue) as boolean;
} catch (error) {
console.error('getDelAppID Failed');
}
return value;
}
// 更新首选项中DelApp的值
async putPreferenceDelApp(value: boolean) {
if (preferenceDelApp !== null) {
try {
await preferenceDelApp.put('DelAppID', value);
await preferenceDelApp.flush();
} catch (error) {
console.error('putDelAppID Failed');
}
}
}
build() {
Column({ space: 5 }) {
Text('自动销毁后台')
.fontSize(18);
Toggle({ type: ToggleType.Switch, isOn: delAppValue })
.onChange(() => {
delAppValue = !delAppValue;
this.putPreferenceDelApp(delAppValue); // 保存到首选项中
this.promptAction.showToast({ message: String(delAppValue) });
});
}
.width('100%')
.height('100%')
.padding({ top: 20 });
}
} 问题现象效果图如下:

let delAppValue: boolean = false; // delAppValue初始化默认值
// 用于让EntryAbility文件获取delAppValue
export function getDelAppValue() {
return delAppValue;
} async aboutToAppear() {
delAppValue = await this.getPreferenceDelApp(); // 获取首选项中的值,同步到全局变量
this.delApp = delAppValue; // 全局变量和开关的值保存一致
this.promptAction.showToast({ message: String(delAppValue) });
} Toggle({ type: ToggleType.Switch, isOn: $$this.delApp })
.onChange((isOn: boolean) => {
delAppValue = isOn; // 开关值发生变化时全局变量同步更新
this.putPreferenceDelApp(delAppValue); // 保存到首选项中
this.promptAction.showToast({ message: String(delAppValue) });
}); onBackground(): void {
// Ability has back to background
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground');
// 判断是否销毁应用
if (getDelAppValue() === true) {
// module.json5配置文件中将removeMissionAfterTerminate字段取值配置为true
this.context.terminateSelf();
}
} 完整代码如下:
import { preferences } from '@kit.ArkData';
import { PromptAction } from '@kit.ArkUI';
let preferenceDelApp: preferences.Preferences | null = null;
let delAppValue: boolean = false; // delAppValue初始化默认值
// 用于让EntryAbility文件获取delAppValue
export function getDelAppValue() {
return delAppValue;
}
@Entry
@Component
struct SetPage {
@State delApp: boolean = false; // 开关,DelApp初始化默认值
uiContext: UIContext = this.getUIContext();
context: Context = this.uiContext.getHostContext() as Context;
promptAction: PromptAction = this.uiContext.getPromptAction();
async aboutToAppear() {
delAppValue = await this.getPreferenceDelApp(); // 获取首选项中的值,同步到全局变量
this.delApp = delAppValue; // 全局变量和开关的值保存一致
this.promptAction.showToast({ message: String(delAppValue) });
}
// 获取首选项中DelApp的值
async getPreferenceDelApp(): Promise<boolean> {
let value: boolean = false;
try {
preferenceDelApp = await preferences.getPreferences(this.context, 'DelAppID');
value = await preferenceDelApp.get('DelAppID', delAppValue) as boolean;
} catch (error) {
console.error('getDelAppID Failed');
}
return value;
}
// 更新首选项中DelApp的值
async putPreferenceDelApp(value: boolean) {
if (preferenceDelApp !== null) {
try {
await preferenceDelApp.put('DelAppID', value);
await preferenceDelApp.flush();
} catch (error) {
console.error('putDelAppID Failed');
}
}
}
build() {
Column({ space: 5 }) {
Text('自动销毁后台')
.fontSize(18);
Toggle({ type: ToggleType.Switch, isOn: $$this.delApp })
.onChange((isOn: boolean) => {
delAppValue = isOn; // 开关值发生变化时全局变量同步更新
this.putPreferenceDelApp(delAppValue); // 保存到首选项中
this.promptAction.showToast({ message: String(delAppValue) });
});
}
.width('100%')
.height('100%')
.padding({ top: 20 });
}
} 效果图如下:
