[Daily HarmonyOS Next Knowledge] Type Judgment, Notch Height, Privacy Popup, Swipe to Next Page Effect, Clear Cache
1. Does instanceof
Judgment Fail in HarmonyOS?
ArkTS partially supports instanceof
. For details, refer to the migration guide:
TypeScript to ArkTS Migration Guide
The instanceof
operator may encounter issues during object transfer:
- Object properties modified during transfer.
- Object references altered during transfer.
- Object constructor not fully executed, leading to incomplete state.
These scenarios can cause incorrect type judgments in the receiving thread. Thus, relying solely on instanceof
for type checking is unsafe.
2. Why Can’t Notch Dimensions Be Retrieved in HarmonyOS?
When using window.getWindowAvoidArea(window.AvoidAreaType.TYPE_CUTOUT)
to get notch dimensions, the data may not be retrieved.
To get navigation bar height, set:
window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR
3. Is There a Privacy Policy Dialog Demo in HarmonyOS?
You can simulate a Dialog
effect using the Stack
component, ensuring the dialog remains visible after page navigation.
Example Code:
import router from '@ohos.router';
@Entry
@Component
struct First {
@State textValue: string = 'Hello World'
@State visible: Visibility = Visibility.None
@State path: string = "pages/Index"
build() {
Stack() {
Row() {
Column() {
Text('Hello World')
.fontSize(50)
.fontWeight(FontWeight.Bold)
Button('click')
.onClick(() => {
console.log("hit me!")
this.visible = this.visible === Visibility.Visible ? Visibility.None : Visibility.Visible
})
.backgroundColor(0x777474)
.fontColor(0x000000)
}
.width('100%')
}
.height('100%')
.backgroundColor(0x885555)
Text('')
.onClick(() => {
this.visible = this.visible === Visibility.Visible ? Visibility.None : Visibility.Visible
})
.width('100%')
.height('100%')
.opacity(0.16)
.backgroundColor(0x000000)
.visibility(this.visible)
Column() {
GridRow({
columns: {
xs: 1,
sm: 4,
md: 8,
lg: 12
},
breakpoints: {
value: ["400vp", "600vp", "800vp"],
reference: BreakpointsReference.WindowSize
},
}) {
GridCol({
span: {
xs: 1,
sm: 2,
md: 4,
lg: 8
},
offset: {
xs: 0,
sm: 1,
md: 2,
lg: 2
}
}) {
Column() {
Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 })
TextInput({ placeholder: '', text: this.textValue }).height(60).width('90%')
.onChange((value: string) => {
this.textValue = value
})
Text('Whether to change a text?').fontSize(16).margin({ bottom: 10 })
Flex({ justifyContent: FlexAlign.SpaceAround }) {
Button('cancel')
.onClick(() => {
this.visible = this.visible === Visibility.Visible ? Visibility.None : Visibility.Visible
}).backgroundColor(0xffffff).fontColor(Color.Black)
Button('jump')
.onClick(() => {
router.pushUrl({
url: this.path
})
}).backgroundColor(0xffffff).fontColor(Color.Red)
}.margin({ bottom: 10 })
}
.backgroundColor(0xffffff)
.visibility(this.visible)
.clip(true)
.borderRadius(20)
}
}
}
}
}
}
4. How to Implement Page-Slide Effect in HarmonyOS?
Reference Code:
(See full code block in the original text for implementation details, including SlideFlipView
, Reader
, and gesture handling.)
Key components:
-
Stack
for overlay effects. -
PanGesture
for swipe detection. -
animateTo
for smooth transitions.
5. How to Clear Cache in HarmonyOS?
To clear app cache:
-
Check Cache Size: Use
storageStatistics.getCurrentBundleStats()
. -
Delete Cache Files:
- Get
cacheDir
viacontext.cacheDir
. - Use
fs
API to check if entries are files or directories and delete them accordingly.
- Get
Example Code:
import fs from '@ohos.file.fs';
let cacheDir = context.cacheDir;
@Entry
@Component
struct Clear_cache {
clearCache() {
fs.listFile(cacheDir).then((filenames) => {
for (let i = 0; i < filenames.length; i++) {
let dirPath = `${cacheDir}/${filenames[i]}`;
let isDirectory;
try {
isDirectory = fs.statSync(dirPath).isDirectory();
} catch (e) {
console.log(e);
}
if (isDirectory) {
fs.rmdirSync(dirPath);
} else {
fs.unlink(dirPath).then(() => {
console.info("remove file succeed");
}).catch((err) => {
console.info("remove file failed with error message: " + err.message + ", error code: " + err.code);
});
}
}
});
}
}
This translation maintains the technical accuracy and structure of the original content while adapting it for an English-speaking audience. Let me know if you need any refinements!
Top comments (0)