Skip to main content

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:

FieldTypeDescription
timestampnumberEvent 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:

FieldTypeDescription
(none)--
TudadaSDK.onHide((res) => {
console.log('App moved to background');
// Pause game, save data, etc.
});

TudadaSDK.offHide();

exitMiniProgram(options)​

Exits the app.

Options:

ParameterTypeRequiredDescription
successfunction-Success callback
failfunction-Failure callback
completefunction-Completion callback
TudadaSDK.exitMiniProgram({
success: () => console.log('Exit request sent'),
});

restartMiniProgram(options)​

Restarts the app.

Options:

ParameterTypeRequiredDescription
successfunction-Success callback
failfunction-Failure callback
completefunction-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:

FieldTypeDescription
namestringEvent name — branch on this value
dataobjectEvent data ({} if none)
timestampnumberEvent time (epoch ms)
TudadaSDK.onPlatformEvent((e) => {
if (e.name === 'promo.open') {
openPromotion(e.data.promoId);
}
});

// Remove all listeners
TudadaSDK.offPlatformEvent();