Skip to content

Back button handling

Listen for the user pressing the back key event and optionally intercept it. Only one listener is active at a time; later registrations override earlier ones.

SuperSDK.onBackPressed(() => {
if (hasUnsavedData) {
showConfirmDialog();
return true; // Return true: intercept and return, the page does not close.
}
return false; // Return false: do not intercept, execute the default return logic
});

Callback Return Value Description:

Return valueBehavior
trueIntercept the back key, keep the page on the current screen, and do not execute the default back logic.
falseDo not intercept; execute the default return logic (go back if there is history, otherwise close the page).
undefined (no return value)Equivalent to false, do not intercept.

Use Case Examples:

// Scenario 1: Confirm before leaving the page
SuperSDK.onBackPressed(() => {
if (formModified) {
const confirmed = confirm('Leave without saving your changes?');
if (!confirmed) {
return true; // User chooses to stay, intercept return
}
}
return false; // Allow return
});
// Scenario 2: Custom return logic (e.g., closing the popup)
SuperSDK.onBackPressed(() => {
if (isModalOpen) {
closeModal();
return true; // Close the pop-up and intercept the back action.
}
return false; // No pop-up, normal return
});

Cancel the back button listener and restore the default back behavior.

SuperSDK.offBackPressed();

Important Notes:

  1. The callback must synchronously return the result; asynchronous operations (async/await) are not supported.
  2. When the callback execution exceeds the timeout (>300ms), the Native SDK automatically executes the default return logic to prevent freezing.
  3. Both the Android physical back button and gesture back will trigger.
  4. On iOS, currently only the navigation bar back button triggers; the swipe-back gesture is not yet supported for interception.