my.onAppShow().
Parameters
Code Example
Remove Specific Listener
Remove All Listeners
AXML with Button
Related APIs
my.onAppShow
Listen for foreground events
my.offAppHide
Stop listening for background events
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Stop listening for events when the Mini Program switches to foreground.
my.onAppShow().
| Property | Type | Required | Description |
|---|---|---|---|
| callback | Function | No | The specific callback to remove. If omitted, all listeners are removed. |
Page({
onLoad() {
// Register listener
my.onAppShow(this.onAppShowHandler);
},
onUnload() {
// Remove specific listener
my.offAppShow(this.onAppShowHandler);
},
onAppShowHandler() {
console.log('App moved to foreground');
}
});
// Remove all onAppShow listeners
my.offAppShow();
<button size="default" onTap="removeListener" type="primary">
Stop listening for foreground events
</button>
Page({
onLoad() {
my.onAppShow(this.onAppShowHandler);
},
onAppShowHandler() {
console.log('App is now in foreground');
},
removeListener() {
my.offAppShow(this.onAppShowHandler);
my.showToast({ content: 'Listener removed' });
}
});
my.offAppShow() that was used with my.onAppShow() to remove only that specific listener.