Skip to main content
Use this API to stop listening for foreground transition events that were registered with my.onAppShow().

Parameters

PropertyTypeRequiredDescription
callbackFunctionNoThe specific callback to remove. If omitted, all listeners are removed.

Code Example

Remove Specific Listener

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 Listeners

// Remove all onAppShow listeners
my.offAppShow();

AXML with Button

<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' });
  }
});
Pass the same callback function reference to my.offAppShow() that was used with my.onAppShow() to remove only that specific listener.

my.onAppShow

Listen for foreground events

my.offAppHide

Stop listening for background events