applibgroup / HarmonyOS-Knowledgebase

This repository contains code samples of HarmonyOS Training
Apache License 2.0
15 stars 6 forks source link

How to save the state of the Custom Dialog? #57

Closed anshulgoel72 closed 3 years ago

anshulgoel72 commented 3 years ago

Describe the query

In my project, I have created a custom dialog inside which, I have a color palette that is initialized with a particular color(say Yellow) already checked. If I click on any color(say Blue), a tick image will come on top of the Blue color and the tick image from the previously checked color(Yellow) will be removed. Now, if I am rotating the emulator screen, the dialog is getting revert back to its initial state (with Yellow color checked). So, how can I save the state of the dialog?

Create the query with harmonyos tag in stackoverflow and share the link here:

https://stackoverflow.com/questions/68594577/how-to-save-the-state-of-the-custom-dialog

Additional information

Developer Platform: Windows
DevEco Studio version: 2.1.0.303
SDK API version: 5
SDK version: 2.1.1.21
Device: Not Required
Device OS version: Not Required

samuel-abhishek commented 3 years ago

Based on the usecase specified, you may use Preferences class in HarmonyOS (ohos.data.preferences.Preferences) to save the color integer that was recently selected by the user when there is a configuration change like rotation of the screen or device language change.

  1. Use

    preferences.putInt(key, value);

    to store the value before the dialog is going to be destroyed.

  2. Use

    preferences.getInt(key, DEFAULT_VALUE);

    to retrieve the value when the dialog is recreated after being destroyed due to configuration change.

For more information, please refer the documentation