调用ApplicationContext.setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_DARK)将应用配色方案设为深色模式后,仅运行期间有效。退出应用重新进入后,设置丢失,深色模式失效。如何实现设置深色模式后,下次进入应用能保留配置?
ApplicationContext.setColorMode属性本身并不支持数据持久化,可以采用用户首选项的数据持久化方式实现配置储存,然后在进入应用时读取用户首选项内容重新配置上一次退出软件时设置的深浅色模式。
import { preferences } from '@kit.ArkData'; export class PreferencesUtil { preference?: preferences.Preferences; getColorModel(context: Context) { this.preference = preferences.getPreferencesSync(context, { name: 'Color' }); } saveColorModel(current: string) { this.preference?.putSync('colorModel', current); this.preference?.flush(); } getChangeColorModel() { let current: string = 'COLOR_MODE_LIGHT'; current = this.preference?.getSync('colorModel', 0) as string; return current; } } let preferenceUtilsObject: PreferencesUtil = new PreferencesUtil(); export default preferenceUtilsObject;
import { ConfigurationConstant } from '@kit.AbilityKit'; import preferenceUtilsObject from './PreferencesUtil'; @Entry @Component struct DarkModePage { build() { Column({ space: 20 }) { Button('切换深色模式') .onClick(() => { this.getUIContext() .getHostContext()?.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_DARK); preferenceUtilsObject.saveColorModel('COLOR_MODE_DARK'); }); Button('切换浅色模式') .onClick(() => { this.getUIContext() .getHostContext()?.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT); preferenceUtilsObject.saveColorModel('COLOR_MODE_LIGHT'); }); Button('跟随系统') .onClick(() => { this.getUIContext() .getHostContext()?.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); preferenceUtilsObject.saveColorModel('COLOR_MODE_NOT_SET'); }); } .justifyContent(FlexAlign.Center) .height('100%') .width('100%'); } }
onWindowStageCreate(windowStage: window.WindowStage): void { // Main window is created, set main page for this ability hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); windowStage.loadContent('pages/Index', () => { preferenceUtilsObject.getColorModel(this.context); let colorModel: string = preferenceUtilsObject.getChangeColorModel(); if (colorModel === 'COLOR_MODE_DARK') { this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_DARK); } else if (colorModel === 'COLOR_MODE_LIGHT') { this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT); } else { this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); } }); }
Q:如何在代码中获取当前系统的深浅色模式状态?
A:在AbilityStage的onCreate()生命周期中获取APP当前的颜色模式:
onCreate(): void { try { this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); console.info( 'colorMode', this.context.config.colorMode); // 获取当前APP的颜色模式 } catch (err) { hilog.error(DOMAIN, 'testTag', 'Failed to set colorMode. Cause: %{public}s', JSON.stringify(err)); } hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); }