Lifecycle API
Lifecycle API
onShow(callback) / offShow(callback?)
Registers/unregisters an event listener for when the app transitions to the foreground. When offShow is called without a callback, all listeners are removed.
Callback Parameters:
| Field | Type | Description |
|---|---|---|
timestamp | number | Event timestamp |
TudadaSDK.onShow((res) => {
console.log('App shown, timestamp:', res.timestamp);
// Resume game, play BGM, etc.
});
// Remove specific callback
TudadaSDK.offShow(myCallback);
// Remove all callbacks
TudadaSDK.offShow();
onHide(callback) / offHide(callback?)
Registers/unregisters an event listener for when the app transitions to the background. When offHide is called without a callback, all listeners are removed.
Callback Parameters:
| Field | Type | Description |
|---|---|---|
| (none) | - | - |
TudadaSDK.onHide((res) => {
console.log('App moved to background');
// Pause game, save data, etc.
});
TudadaSDK.offHide();
exitMiniProgram(options)
Exits the app.
Options:
| Parameter | Type | Required | Description |
|---|---|---|---|
success | function | - | Success callback |
fail | function | - | Failure callback |
complete | function | - | Completion callback |
TudadaSDK.exitMiniProgram({
success: () => console.log('Exit request sent'),
});
restartMiniProgram(options)
Restarts the app.
Options:
| Parameter | Type | Required | Description |
|---|---|---|---|
success | function | - | Success callback |
fail | function | - | Failure callback |
complete | function | - | Completion callback |
TudadaSDK.restartMiniProgram({
success: () => console.log('Restart request sent'),
});
onPlatformEvent(callback) / offPlatformEvent(callback?)
Subscribes to / unsubscribes from custom events the platform (host) delivers to the running game. Data available at launch comes from getLaunchOptions(); data that arrives while the game is running comes through this event. Which name/data values are sent is agreed with your platform contact. No production host currently sends any event (the channel is reserved for future use); data delivered at launch, such as an invite code, comes from getLaunchOptions().
Events that arrive while no listener is registered are buffered (up to 32) and delivered in order to the next callback that registers. Calling offPlatformEvent without a callback removes all listeners.
Callback Parameters:
| Field | Type | Description |
|---|---|---|
name | string | Event name — branch on this value |
data | object | Event data ({} if none) |
timestamp | number | Event time (epoch ms) |
TudadaSDK.onPlatformEvent((e) => {
if (e.name === 'promo.open') {
openPromotion(e.data.promoId);
}
});
// Remove all listeners
TudadaSDK.offPlatformEvent();